From c5c9ba107e89337f3b683d9ab243b96d9b709b49 Mon Sep 17 00:00:00 2001 From: Tomasz Konojacki Date: Sun, 31 May 2020 06:54:50 +0200 Subject: [PATCH] sddsasad --- cxx/system_metrics.hxx | 16 ++++++++-------- cxx/win32/win32.cxx | 25 ++++++++++++++----------- lib/System/Metrics.xs | 40 +++++++++++++++++++++------------------- 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/cxx/system_metrics.hxx b/cxx/system_metrics.hxx index 170d4a9..1218997 100644 --- a/cxx/system_metrics.hxx +++ b/cxx/system_metrics.hxx @@ -59,6 +59,13 @@ struct process { double cpu_usage = -1; }; +struct process_list_fields { + bool pid = true; + bool cpu_usage = false; + bool name = false; + SM_MAPIFY_STRUCT(bool, pid, cpu_usage, name) +}; + class system_metrics { public: // platform specific: @@ -69,7 +76,7 @@ public: uint64_t number_of_cpus(); uint64_t page_size(); struct mem_info mem_info(); - std::vector process_list(); + std::vector process_list(const process_list_fields &fields); // common double cpu_usage(); @@ -82,11 +89,4 @@ private: std::unordered_map prev_proc_cpu_times; }; -struct process_list_fields { - bool pid = true; - bool cpu_usage = false; - bool name = false; - SM_MAPIFY_STRUCT(bool, pid, cpu_usage, name) -}; - double calc_cpu_usage(struct cpu_times &prev_ct, const struct cpu_times &ct); diff --git a/cxx/win32/win32.cxx b/cxx/win32/win32.cxx index 0533e6b..c915188 100644 --- a/cxx/win32/win32.cxx +++ b/cxx/win32/win32.cxx @@ -154,7 +154,7 @@ system_metrics::mem_info() } std::vector -system_metrics::process_list() +system_metrics::process_list(const process_list_fields &fields) { auto &buf = this->_priv->proc_info_buf; @@ -191,20 +191,23 @@ system_metrics::process_list() struct process p; p.pid = reinterpret_cast(info->UniqueProcessId); - unicode_string_to_string(info->ImageName, p.path); - struct cpu_times t; - t.user = info->UserTime.QuadPart; - t.system = info->KernelTime.QuadPart; - t.idle = ct.system + ct.idle + ct.user - t.user - t.system; + if (fields.name) + unicode_string_to_string(info->ImageName, p.path); - p.cpu_usage = calc_cpu_usage( - this->prev_proc_cpu_times[p.pid], - t - ); + if (fields.cpu_usage) { + struct cpu_times t; + t.user = info->UserTime.QuadPart; + t.system = info->KernelTime.QuadPart; + t.idle = ct.system + ct.idle + ct.user - t.user - t.system; - ret.push_back(p); + p.cpu_usage = calc_cpu_usage( + this->prev_proc_cpu_times[p.pid], + t + ); + } + ret.push_back(p); if (info->NextEntryOffset) info = reinterpret_cast( diff --git a/lib/System/Metrics.xs b/lib/System/Metrics.xs index c94bf4d..85cd312 100644 --- a/lib/System/Metrics.xs +++ b/lib/System/Metrics.xs @@ -76,9 +76,9 @@ extern "C" { template static inline void -unpack_args(pTHX_ T &args_struct) +unpack_args(pTHX_ T &args_struct, SV **mark) { - dXSARGS; + dSP; dAX; dITEMS; if (items%2==0) croak("uneven"); @@ -202,13 +202,14 @@ CODE: SM_MAPIFY_STRUCT(SV*, fields) } args; - unpack_args(aTHX_ args); + unpack_args(aTHX_ args, mark); process_list_fields fields; if (args.fields) { + // FIXME: pusty arrayref! AV *fields_arr = deref_av(args.fields); - for (size_t i = 0; AvFILL(fields_arr) >= i; i++) { + for (SSize_t i = 0; AvFILL(fields_arr) >= i; i++) { STRLEN len; char *key = SvPV(AvARRAY(fields_arr)[i], len); @@ -223,24 +224,25 @@ CODE: fields.set_all(true); } - printf("pid: %d\ncpu_usage: %d\nname: %d", (int)fields.pid, (int)fields.cpu_usage, (int)fields.name); - - std::vector proc_list; + AV* ret; SM_BEGIN_WRAP_EXCEPTION - proc_list = THIS->process_list(); - SM_END_WRAP_EXCEPTION + auto proc_list = THIS->process_list(fields); - AV* ret = newAV(); - av_fill(ret, proc_list.size() - 1); - size_t i = 0; - for (const auto &p: proc_list) { - HV *proc_hv = newHV(); - hv_stores(proc_hv, "pid", newSVuv(p.pid)); - hv_stores(proc_hv, "path", newSVpvn_flags(p.path.data(), p.path.size(), 0)); - hv_stores(proc_hv, "cpu_usage", newSVnv(p.cpu_usage)); - AvARRAY(ret)[i++] = newRV_noinc((SV*)proc_hv); - } + ret = newAV(); + av_fill(ret, proc_list.size() - 1); + size_t i = 0; + for (const auto &p: proc_list) { + HV *proc_hv = newHV(); + hv_stores(proc_hv, "pid", newSVuv(p.pid)); + if (fields.name) + hv_stores(proc_hv, "path", newSVpvn_flags(p.path.data(), p.path.size(), 0)); + if (fields.cpu_usage) + hv_stores(proc_hv, "cpu_usage", newSVnv(p.cpu_usage)); + + AvARRAY(ret)[i++] = newRV_noinc(reinterpret_cast(proc_hv)); + } + SM_END_WRAP_EXCEPTION RETVAL = ret; OUTPUT: RETVAL -- 2.11.0