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