Skip to content

Commit

Permalink
feat (sampling): Put kernel threads in own group.
Browse files Browse the repository at this point in the history
Put all kernel threads in the same regions group based on
whether /proc/[pid]/cmdline is empty (this is also how ps checks if
something is a kernel thread)

This makes it easier to distinguish kernel threads from normal threads
in visualization utilites such as Vampir
  • Loading branch information
cvonelm committed Aug 21, 2024
1 parent fdc47af commit 207cbea
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/lo2s/trace/trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ class Trace
otf2::definition::comm_locations_group& hardware_comm_locations_group_;
otf2::definition::regions_group& lo2s_regions_group_;
otf2::definition::regions_group& syscall_regions_group_;
otf2::definition::regions_group& kernel_regions_group_;

otf2::definition::detail::weak_ref<otf2::definition::metric_class> cpuid_metric_class_;
std::map<std::set<Cpu>, otf2::definition::detail::weak_ref<otf2::definition::metric_class>>
Expand Down
9 changes: 8 additions & 1 deletion include/lo2s/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Process;
class Thread
{
public:
explicit Thread(pid_t tid) : tid_(tid)
explicit Thread(pid_t tid, bool is_kernel_thread = false)
: tid_(tid), kernel_thread_(is_kernel_thread)
{
}

Expand Down Expand Up @@ -77,6 +78,11 @@ class Thread
return Thread(-1);
}

bool is_kernel_thread()
{
return kernel_thread_;
}

friend std::ostream& operator<<(std::ostream& stream, const Thread& thread)
{
return stream << fmt::format("{}", thread);
Expand All @@ -89,6 +95,7 @@ class Thread

private:
pid_t tid_;
bool kernel_thread_;
};

class Process
Expand Down
7 changes: 7 additions & 0 deletions src/trace/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ Trace::Trace()
syscall_regions_group_(registry_.create<otf2::definition::regions_group>(
intern("<syscalls>"), otf2::common::paradigm_type::user,
otf2::common::group_flag_type::none)),
kernel_regions_group_(registry_.create<otf2::definition::regions_group>(
intern("<kernel threads>"), otf2::common::paradigm_type::user,
otf2::common::group_flag_type::none)),
system_tree_root_node_(registry_.create<otf2::definition::system_tree_node>(
intern(nitro::env::hostname()), intern("machine"))),
groups_(ExecutionScopeGroup::instance())
Expand Down Expand Up @@ -759,6 +762,10 @@ void Trace::add_thread_exclusive(Thread thread, const std::string& name,
ByThread(thread), iname, iname, iname, otf2::common::role_type::function,
otf2::common::paradigm_type::user, otf2::common::flags_type::none, iname, 0, 0);

if (thread.is_kernel_thread())
{
kernel_regions_group_.add_member(thread_region);
}
// create calling context
auto& thread_cctx = registry_.create<otf2::definition::calling_context>(
ByThread(thread), thread_region, otf2::definition::source_code_location());
Expand Down
25 changes: 24 additions & 1 deletion src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,35 @@ std::unordered_map<Thread, std::string> get_comms_for_running_threads()
{
continue;
}

std::ifstream cmdline(entry.path() / "cmdline");

std::string name = get_process_comm(process);

std::string cmdline_str = "";
if (cmdline.good())
{
cmdline >> cmdline_str;

// Kernel threads can be distinguished from normal threads by the empty
// /proc/[pid]/cmdline file. This is how the ps utility does it.
if (cmdline_str == "")
{
ret.emplace(Thread(std::stoi(entry.path().filename().string()), true), name);
}
else
{
ret.emplace(Thread(std::stoi(entry.path().filename().string()), false), name);
}
}
else
{
ret.emplace(Thread(std::stoi(entry.path().filename().string()), false), name);
}

scope_group.add_process(process);

Log::trace() << "mapping from /proc/" << process.as_pid_t() << ": " << name;
ret.emplace(process.as_thread(), name);
try
{
std::filesystem::path task(fmt::format("/proc/{}/task", process.as_pid_t()));
Expand Down

0 comments on commit 207cbea

Please sign in to comment.