r11579: syncing up perf counter code cfrom trunk
[kai/samba-autobuild/.git] / examples / perfcounter / perf_writer_mem.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Performance Counter Daemon
4  *
5  *  Copyright (C) Marcin Krzysztof Porwit    2005
6  *  
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *  
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "perf.h"
23
24 void get_meminfo(PERF_DATA_BLOCK *data)
25 {
26     int status;
27     struct sysinfo info;
28     status = sysinfo(&info);
29     
30     data->memInfo.data->availPhysKb = (info.freeram * info.mem_unit)/1024;
31     data->memInfo.data->availSwapKb = (info.freeswap * info.mem_unit)/1024;
32     data->memInfo.data->totalPhysKb = (info.totalram * info.mem_unit)/1024;
33     data->memInfo.data->totalSwapKb = (info.totalswap * info.mem_unit)/1024;
34
35     /* Also get uptime since we have the structure */
36     data->PerfTime = (unsigned long)info.uptime;
37
38     return;
39 }
40
41 void init_memdata_desc(PERF_DATA_BLOCK *data)
42 {
43     init_perf_counter(&(data->memInfo.memObjDesc),
44                       &(data->memInfo.memObjDesc),
45                       get_counter_id(data),
46                       "Memory",
47                       "The Memory performance object consists of counters that describe the behavior of physical and virtual memory on the computer.",
48                       0,
49                       PERF_OBJECT);
50     init_perf_counter(&(data->memInfo.availPhysKb),
51                       &(data->memInfo.memObjDesc),
52                       get_counter_id(data),
53                       "Available Physical Kilobytes",
54                       "Available Physical Kilobytes is the number of free kilobytes in physical memory",
55                       PERF_SIZE_DWORD | PERF_TYPE_NUMBER | PERF_NUMBER_DECIMAL | PERF_DISPLAY_NO_SUFFIX,
56                       PERF_COUNTER);
57     init_perf_counter(&(data->memInfo.availSwapKb),
58                       &(data->memInfo.memObjDesc),
59                       get_counter_id(data),
60                       "Available Swap Kilobytes",
61                       "Available Swap Kilobytes is the number of free kilobytes in swap space",
62                       PERF_SIZE_DWORD | PERF_TYPE_NUMBER | PERF_NUMBER_DECIMAL | PERF_DISPLAY_NO_SUFFIX,
63                       PERF_COUNTER);
64     init_perf_counter(&(data->memInfo.totalPhysKb),
65                       &(data->memInfo.memObjDesc),
66                       get_counter_id(data),
67                       "Total Physical Kilobytes",
68                       "Total Physical Kilobytes is a base counter",
69                       PERF_SIZE_DWORD | PERF_TYPE_NUMBER | PERF_NUMBER_DECIMAL | PERF_COUNTER_BASE | PERF_DISPLAY_NOSHOW,
70                       PERF_COUNTER);
71     init_perf_counter(&(data->memInfo.totalSwapKb),
72                       &(data->memInfo.memObjDesc),
73                       get_counter_id(data),
74                       "Total Swap Kilobytes",
75                       "Total Swap Kilobytes is a base counter",
76                       PERF_SIZE_DWORD | PERF_TYPE_NUMBER | PERF_NUMBER_DECIMAL | PERF_COUNTER_BASE | PERF_DISPLAY_NOSHOW,
77                       PERF_COUNTER);
78
79     return;
80 }
81
82 void init_mem_data(PERF_DATA_BLOCK *data)
83 {
84     data->memInfo.data = calloc(1, sizeof(*data->memInfo.data));
85     if(!data->memInfo.data)
86     {
87         perror("init_memdata: out of memory");
88         exit(1);
89     }
90
91     init_memdata_desc(data);
92
93     get_meminfo(data);
94
95     return;
96 }
97
98 void output_mem_desc(PERF_DATA_BLOCK *data, RuntimeSettings rt)
99 {
100     output_perf_desc(data->memInfo.memObjDesc, rt);
101     output_perf_desc(data->memInfo.availPhysKb, rt);
102     output_perf_desc(data->memInfo.availSwapKb, rt);
103     output_perf_desc(data->memInfo.totalPhysKb, rt);
104     output_perf_desc(data->memInfo.totalSwapKb, rt);
105
106     return;
107 }
108
109 void output_meminfo(PERF_DATA_BLOCK *data, RuntimeSettings rt, int tdb_flags)
110 {
111     output_perf_counter(data->memInfo.availPhysKb, 
112                         (unsigned long long)data->memInfo.data->availPhysKb, 
113                         rt, tdb_flags);
114     output_perf_counter(data->memInfo.availSwapKb, 
115                         (unsigned long long)data->memInfo.data->availSwapKb,
116                         rt, tdb_flags);
117     output_perf_counter(data->memInfo.totalPhysKb,
118                         (unsigned long long)data->memInfo.data->totalPhysKb,
119                         rt, tdb_flags);
120     output_perf_counter(data->memInfo.totalSwapKb,
121                         (unsigned long long)data->memInfo.data->totalSwapKb,
122                         rt, tdb_flags);
123
124     return;
125 }