summaryrefslogtreecommitdiff
path: root/examples/perfcounter/perf_writer_cpu.c
blob: eed69de0332927c88e9543f01cce4f16f78bfdf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* 
 *  Unix SMB/CIFS implementation.
 *  Performance Counter Daemon
 *
 *  Copyright (C) Marcin Krzysztof Porwit    2005
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "perf.h"

void init_cpudata_desc(PERF_DATA_BLOCK *data)
{
    init_perf_counter(&(data->cpuInfo.cpuObjDesc),
		      &(data->cpuInfo.cpuObjDesc),
		      get_counter_id(data),
		      "Processor",
		      "The Processor object consists of counters that describe the behavior of the CPU.",
		      0,
		      PERF_OBJECT);
    init_perf_counter(&(data->cpuInfo.userCPU),
		      &(data->cpuInfo.cpuObjDesc),
		      get_counter_id(data),
		      "\% User CPU Utilization",
		      "\% User CPU Utilization is the percentage of the CPU used by  processes executing user code.",
		      PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | PERF_DISPLAY_PERCENT,
		      PERF_COUNTER);
    init_perf_counter(&(data->cpuInfo.systemCPU),
		      &(data->cpuInfo.cpuObjDesc),
		      get_counter_id(data),
		      "\% System CPU Utilization",
		      "\% System CPU Utilization is the percentage of the CPU used by processes doing system calls.",
		      PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | PERF_DISPLAY_PERCENT,
		      PERF_COUNTER);
    init_perf_counter(&(data->cpuInfo.niceCPU),
		      &(data->cpuInfo.cpuObjDesc),
		      get_counter_id(data),
		      "\% Nice CPU Utilization",
		      "\% Nice CPU Utilization is the percentage of the CPU used by processes running in nice mode.",
		      PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | PERF_DISPLAY_NOSHOW,
		      PERF_COUNTER);
    init_perf_counter(&(data->cpuInfo.idleCPU),
		      &(data->cpuInfo.cpuObjDesc),
		      get_counter_id(data),
		      "\% Idle CPU",
		      "\% Idle CPU is the percentage of the CPU not doing any work.",
		      PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | PERF_DISPLAY_NOSHOW,
		      PERF_COUNTER);

    return;
}

void get_cpuinfo(PERF_DATA_BLOCK *data)
{
    int num, i;
    unsigned int cpuid;
    char buf[PROC_BUF];
    static FILE *fp = NULL;

    if(!fp)
    {
	if(!(fp = fopen("/proc/stat", "r")))
	{
	    perror("get_cpuinfo: fopen");
	    exit(1);
	}
    }

    rewind(fp);
    fflush(fp);

    /* Read in the first line and discard it -- that has the CPU summary */
    if(!fgets(buf, sizeof(buf), fp))
    {
	perror("get_cpuinfo: fgets");
	exit(1);
    }
    for(i = 0; i < data->cpuInfo.numCPUs; i++)
    {
	if(!fgets(buf, sizeof(buf), fp))
	{
	    perror("get_cpuinfo: fgets");
	    exit(1);
	}
	num = sscanf(buf, "cpu%u %Lu %Lu %Lu %Lu",
		     &cpuid,
		     &data->cpuInfo.data[i].user,
		     &data->cpuInfo.data[i].nice,
		     &data->cpuInfo.data[i].system,
		     &data->cpuInfo.data[i].idle);
	if(i != cpuid)
	{
	    perror("get_cpuinfo: /proc/stat inconsistent?");
	    exit(1);
	}
	/*
	  Alternate way of doing things:
	  struct tms buffer;
	  data->PerfTime100nSec = times(&buffer);
	*/
	data->PerfTime100nSec += data->cpuInfo.data[i].user +
	    data->cpuInfo.data[i].nice +
	    data->cpuInfo.data[i].system +
	    data->cpuInfo.data[i].idle;
    }
    data->PerfTime100nSec /= data->cpuInfo.numCPUs;
    return;
}

void init_cpu_data(PERF_DATA_BLOCK *data)
{
    data->cpuInfo.data = calloc(data->cpuInfo.numCPUs, sizeof(*data->cpuInfo.data));
    if(!data->cpuInfo.data)
    {
	perror("init_cpu_data: out of memory");
	exit(1);
    }

    init_cpudata_desc(data);

    get_cpuinfo(data);

    return;
}   

void output_cpu_desc(PERF_DATA_BLOCK *data, RuntimeSettings rt)
{
    output_perf_desc(data->cpuInfo.cpuObjDesc, rt);
    output_perf_desc(data->cpuInfo.userCPU, rt);
    output_perf_desc(data->cpuInfo.niceCPU, rt);
    output_perf_desc(data->cpuInfo.systemCPU, rt);
    output_perf_desc(data->cpuInfo.idleCPU, rt);
    if(data->cpuInfo.numCPUs > 1)
	    output_num_instances(data->cpuInfo.cpuObjDesc, data->cpuInfo.numCPUs + 1, rt);

    return;
}

void output_cpuinfo(PERF_DATA_BLOCK *data, RuntimeSettings rt, int tdb_flags)
{
    int i;
    char buf[NAME_LEN];

    output_perf_counter(data->cpuInfo.userCPU,
			data->cpuInfo.data[0].user,
			rt, tdb_flags);
    output_perf_counter(data->cpuInfo.systemCPU,
			data->cpuInfo.data[0].system,
			rt, tdb_flags);
    output_perf_counter(data->cpuInfo.niceCPU,
			data->cpuInfo.data[0].nice,
			rt, tdb_flags);
    output_perf_counter(data->cpuInfo.idleCPU,
			data->cpuInfo.data[0].idle,
			rt, tdb_flags);
    if(data->cpuInfo.numCPUs > 1)
      {
	      for(i = 0; i < data->cpuInfo.numCPUs; i++)
	      {
		      memset(buf, 0, NAME_LEN);
		      sprintf(buf, "cpu%d", i);
		      output_perf_instance(data->cpuInfo.cpuObjDesc.index,
					   i,
					   (void *)&(data->cpuInfo.data[i]),
					   sizeof(data->cpuInfo.data[i]),
					   buf, rt, tdb_flags);
	      }
	      
	      memset(buf, 0, NAME_LEN);
	      sprintf(buf, "_Total");
	      output_perf_instance(data->cpuInfo.cpuObjDesc.index,
				   i,
				   (void *)&(data->cpuInfo.data[i]),
				   sizeof(data->cpuInfo.data[i]),
				   buf, rt, tdb_flags);
      }
    return;
}