• R/O
  • HTTP
  • SSH
  • HTTPS

system-metrics: Commit


Commit MetaInfo

Revisión2b06a0ddc3db50acefe1726be2d6b9840b79890a (tree)
Tiempo2020-05-24 06:50:34
AutorTomasz Konojacki <me@xenu...>
CommiterTomasz Konojacki

Log Message

sdasdsad

Cambiar Resumen

Diferencia incremental

--- a/cxx/common/common.cxx
+++ b/cxx/common/common.cxx
@@ -22,7 +22,6 @@
2222
2323 #include "system_metrics.hxx"
2424
25-static double calc_cpu_usage(struct cpu_times &prev_ct, const struct cpu_times &ct);
2625
2726 double
2827 system_metrics::cpu_usage()
@@ -47,11 +46,13 @@ system_metrics::per_cpu_usage()
4746 return ret;
4847 }
4948
49+
50+
5051 //
5152 // util functions
5253 //
5354
54-static double
55+double
5556 calc_cpu_usage(struct cpu_times &prev_ct, const struct cpu_times &ct)
5657 {
5758 uint64_t idle = ct.idle;
--- a/cxx/sm_mapify.hxx
+++ b/cxx/sm_mapify.hxx
@@ -63,7 +63,7 @@
6363 // () is a magic value that signals the end of the list, see _SM_DISCARD_IF_END().
6464 #define _SM_FOREACH(...) _SM_FOREACH2(__VA_ARGS__, ())
6565
66-#define _SM_ADD_FIELD(field_name) { #field_name , &std::remove_pointer<decltype(this)>::type::name }
66+#define _SM_ADD_FIELD(field_name) { #field_name , &std::remove_pointer<decltype(this)>::type::field_name }
6767
6868 // SM_MAPIFY_STRUCT(type, field1, ...) is a macro that adds std::map-like at()
6969 // and contains() methods to a struct for specified fields. All mapified fields
@@ -102,5 +102,5 @@
102102 } \
103103 bool contains(const std::string_view &field) { \
104104 return static_cast<bool>(this->_sm_fields_map().count(field)); \
105- } \
106- }
105+ }
106+
--- a/cxx/system_metrics.hxx
+++ b/cxx/system_metrics.hxx
@@ -22,11 +22,15 @@
2222
2323 #pragma once
2424
25-#include <cstdef>
25+#include <cstddef>
2626 #include <memory>
2727 #include <string>
2828 #include <vector>
29+#include <unordered_map>
2930 #include <stdint.h>
31+#include <string_view>
32+
33+#include "sm_mapify.hxx"
3034
3135 #if defined(__GNUC__) || defined(__clang__)
3236 # define SM_LIKELY(x) __builtin_expect(!!(x),1)
@@ -52,6 +56,7 @@ struct process {
5256 uint64_t pid = 0;
5357 uint64_t ppid = 0;
5458 std::string path;
59+ double cpu_usage = -1;
5560 };
5661
5762 class system_metrics {
@@ -74,4 +79,14 @@ private:
7479 struct cpu_times prev_cpu_times;
7580 std::vector<struct cpu_times> prev_per_cpu_times;
7681 std::unique_ptr<struct sm_private> _priv;
82+ std::unordered_map<uint64_t, struct cpu_times > prev_proc_cpu_times;
83+};
84+
85+struct process_list_fields {
86+ bool pid = true;
87+ bool cpu_usage;
88+ bool name;
89+ SM_MAPIFY_STRUCT(bool, pid, cpu_usage, name)
7790 };
91+
92+double calc_cpu_usage(struct cpu_times &prev_ct, const struct cpu_times &ct);
--- a/cxx/win32/win32.cxx
+++ b/cxx/win32/win32.cxx
@@ -24,7 +24,6 @@
2424 #include "win32_exception.hxx"
2525
2626 #include <string>
27-#include <vector>
2827 #include <mutex>
2928
3029 #include <psapi.h>
@@ -159,8 +158,11 @@ system_metrics::process_list()
159158 {
160159 auto &buf = this->_priv->proc_info_buf;
161160
161+ struct cpu_times ct;
162162 for (;;) {
163163 ULONG ret_size_bytes;
164+
165+ ct = this->cpu_times();
164166 NTSTATUS status = NtQuerySystemInformation(
165167 SystemProcessInformation,
166168 buf.data(),
@@ -181,6 +183,8 @@ system_metrics::process_list()
181183 break;
182184 }
183185
186+ uint64_t total_cpu;
187+
184188 SYSTEM_PROCESS_INFORMATION *info = reinterpret_cast<SYSTEM_PROCESS_INFORMATION*>(&buf[0]);
185189 std::vector<struct process> ret;
186190 for (;;) {
@@ -189,8 +193,19 @@ system_metrics::process_list()
189193 p.pid = reinterpret_cast<uint64_t>(info->UniqueProcessId);
190194 unicode_string_to_string(info->ImageName, p.path);
191195
196+ struct cpu_times t;
197+ t.user = info->UserTime.QuadPart;
198+ t.system = info->KernelTime.QuadPart;
199+ t.idle = ct.system + ct.idle + ct.user - t.user - t.system;
200+
201+ p.cpu_usage = calc_cpu_usage(
202+ this->prev_proc_cpu_times[p.pid],
203+ t
204+ );
205+
192206 ret.push_back(p);
193207
208+
194209 if (info->NextEntryOffset)
195210 info = reinterpret_cast<SYSTEM_PROCESS_INFORMATION*>(
196211 reinterpret_cast<std::byte*>(info) + info->NextEntryOffset
--- a/lib/System/Metrics.xs
+++ b/lib/System/Metrics.xs
@@ -167,8 +167,17 @@ OUTPUT:
167167 RETVAL
168168
169169 AV*
170-system_metrics::process_list()
170+system_metrics::process_list(...)
171171 CODE:
172+ //if (items%2==0)
173+ // croak("uneven");
174+ process_list_fields fields;
175+ for (size_t i = 1; items > i; i+=1) {
176+ STRLEN len;
177+ char *key = SvPV(ST(i), len);
178+ fields.at({key,len}) = true;
179+ }
180+ printf("pid: %d\ncpu_usage: %d\nname: %d", (int)fields.pid, (int)fields.cpu_usage, (int)fields.name);
172181 std::vector<struct process> proc_list;
173182 SM_BEGIN_WRAP_EXCEPTION
174183 proc_list = THIS->process_list();
@@ -180,6 +189,7 @@ CODE:
180189 HV *proc_hv = newHV();
181190 hv_stores(proc_hv, "pid", newSVuv(p.pid));
182191 hv_stores(proc_hv, "path", newSVpvn_flags(p.path.data(), p.path.size(), 0));
192+ hv_stores(proc_hv, "cpu_usage", newSVnv(p.cpu_usage));
183193 AvARRAY(ret)[i++] = newRV_noinc((SV*)proc_hv);
184194 }
185195 RETVAL = ret;
Show on old repository browser