formatting changes in cpusleep
[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 / s. For that, derive type was
31  selected and the time difference between BOOT and MONOTONIC clocks
32  fed to RRD
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, "cpusleep", 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   double db = b.tv_sec + 1e-9 * b.tv_nsec;
73   double dm = m.tv_sec + 1e-9 * m.tv_nsec;
74
75   // to avoid false positives in counter overflow due to reboot,
76   // derive is used
77   derive_t sleep = (derive_t) ((db-dm) * 1000);  
78
79   cpusleep_submit(sleep);
80
81   return (0);
82 }
83
84 void module_register(void)
85 {
86   plugin_register_read("cpusleep", cpusleep_read);
87 } /* void module_register */
88
89
90 /*
91  * Local variables:
92  *  c-file-style: "gnu"
93  *  indent-tabs-mode: nil
94  *  c-indent-level: 4
95  *  c-basic-offset: 2
96  *  tab-width: 4
97  * End:
98  */