| 796 | const NumaSplitPlan& active_numa_split() { return g_numa_plan; } |
| 797 | |
| 798 | void ExpertPool::diag(std::FILE* f) const { |
| 799 | const uint64_t h = head_.load(); |
| 800 | uint32_t batch = (uint32_t) (h >> 32), claimed = (uint32_t) h & 0xffffu, total = (uint32_t) (h >> 16) & 0xffffu, |
| 801 | done = done_.load(); |
| 802 | // CBA: this summed-slices branch is never run by a test (it needs a diag call mid-phase), it is a trivial sum, a mid-phase |
| 803 | // diag test if it ever misreports |
| 804 | if (numa_n_ > 0 && mode_ >= 5) { // a split native batch never writes head_/done_: the slices' sums instead |
| 805 | claimed = total = done = 0; |
| 806 | for (int k = 0; k < numa_n_; ++k) { |
| 807 | const uint64_t sh = nsync_[(size_t) k].head.load(); |
| 808 | batch = (uint32_t) (sh >> 32); // every slice head carries the batch's epoch |
| 809 | claimed += (uint32_t) sh & 0xffffu; |
| 810 | total += (uint32_t) (sh >> 16) & 0xffffu; |
| 811 | done += nsync_[(size_t) k].done.load(); |
| 812 | } |
| 813 | } |
| 814 | std::fprintf(f, " expert pool: epoch %u, batch epoch %u: %u of %u jobs claimed, %u done; %u of %d workers parked, " |
| 815 | "%u sleeping; mode %d\n", epoch_.load(), batch, claimed, total, done, parked_count(), n_, |
| 816 | sleepers_.load(), mode_); |
| 817 | if (numa_n_ > 0) std::fprintf(f, " expert pool NUMA slices:%s\n", numa_counters().c_str()); |
| 818 | std::fprintf(f, " expert pool threads:"); |
| 819 | for (int i = 0; i < n_; ++i) { |
| 820 | const int32_t s = wstate_[(size_t) i].load(); |
| 821 | if (s == kParked) std::fprintf(f, " w%d=parked", i); |
| 822 | else if (s == kSleeping) std::fprintf(f, " w%d=sleeping", i); |
| 823 | else if (s == kBetween) std::fprintf(f, " w%d=draining", i); |
| 824 | else std::fprintf(f, " w%d=job%d", i, s); |
| 825 | } |
| 826 | const int32_t hs = hstate_.load(); |
| 827 | const char* hn = hs == kIdle ? "idle" : hs == kWaitParked ? "waiting for the workers to park" |
| 828 | : hs == kWaitDone ? "waiting for the jobs to finish" : "running a job"; |
| 829 | std::fprintf(f, "; host %s", hn); |
| 830 | if (hs >= 0) std::fprintf(f, " %d", hs); |
| 831 | std::fprintf(f, " for %lld ms\n", (long long) (now_ms() - hstate_ms_.load())); |
| 832 | #if defined(__linux__) |
| 833 | // the kernel's view of every thread of the process (no debugger needed): tid, name, run state (R running, S sleeping, D disk wait), the CPU it last ran on and what |
| 834 | // it waits in: a worker that spins shows R, one in the futex of the sleep shows S / futex_wait_queue (issue #35, the pool_tasks_test stall) |
| 835 | if (DIR* d = opendir("/proc/self/task")) { |
| 836 | std::fprintf(f, " threads [tid name state cpu wchan]:"); |
| 837 | while (const dirent* de = readdir(d)) { |
| 838 | if (de->d_name[0] == '.') continue; |
| 839 | char path[96], line[512] = {0}, wchan[64] = {0}; |
| 840 | std::snprintf(path, sizeof path, "/proc/self/task/%s/stat", de->d_name); |
| 841 | if (std::FILE* sf = std::fopen(path, "r")) { if (!std::fgets(line, sizeof line, sf)) line[0] = 0; std::fclose(sf); } |
| 842 | std::snprintf(path, sizeof path, "/proc/self/task/%s/wchan", de->d_name); |
| 843 | if (std::FILE* wf = std::fopen(path, "r")) { if (!std::fgets(wchan, sizeof wchan, wf)) wchan[0] = 0; std::fclose(wf); } |
| 844 | const char* lp = std::strchr(line, '('); |
| 845 | const char* rp = std::strrchr(line, ')'); // "tid (name) S ...": the name can hold spaces, so the fields are counted from the last ')' |
| 846 | char name[20] = {0}; |
| 847 | if (lp != nullptr && rp != nullptr && rp > lp) std::snprintf(name, sizeof name, "%.*s", (int) (std::min<std::ptrdiff_t>(rp - lp - 1, 15)), lp + 1); |
| 848 | char state = '?'; |
| 849 | int cpu = -1; |
| 850 | if (rp != nullptr && rp[1] == ' ') { |
| 851 | state = rp[2]; |
| 852 | int field = 3; // field 3 is the state; the CPU is field 39 |
| 853 | const char* q = rp + 2; |
| 854 | while (*q != 0 && field < 39) { if (*q == ' ') ++field; ++q; } |
| 855 | if (*q != 0) cpu = std::atoi(q); |