2 * collectd - src/cpusleep.c
3 * Copyright (C) 2016 rinigus
5 * The MIT License (MIT)
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * rinigus <http://github.com/rinigus>
28 * CPU sleep is reported in milliseconds of sleep per second of wall
29 * time. For that, the time difference between BOOT and MONOTONIC clocks
30 * is reported using derive type.
39 static void cpusleep_submit(derive_t cpu_sleep) {
40 value_list_t vl = VALUE_LIST_INIT;
42 vl.values = &(value_t){.derive = cpu_sleep};
44 sstrncpy(vl.plugin, "cpusleep", sizeof(vl.plugin));
45 sstrncpy(vl.type, "total_time_in_ms", sizeof(vl.type));
47 plugin_dispatch_values(&vl);
50 static int cpusleep_read(void) {
52 if (clock_gettime(CLOCK_BOOTTIME, &b) < 0) {
53 ERROR("cpusleep plugin: clock_boottime failed");
57 if (clock_gettime(CLOCK_MONOTONIC, &m) < 0) {
58 ERROR("cpusleep plugin: clock_monotonic failed");
62 // to avoid false positives in counter overflow due to reboot,
63 // derive is used. Sleep is calculated in milliseconds
64 derive_t diffsec = b.tv_sec - m.tv_sec;
65 derive_t diffnsec = b.tv_nsec - m.tv_nsec;
66 derive_t sleep = diffsec * 1000 + diffnsec / 1000000;
68 cpusleep_submit(sleep);
73 void module_register(void) {
74 plugin_register_read("cpusleep", cpusleep_read);
75 } /* void module_register */