summaryrefslogtreecommitdiffstats
path: root/examples/perfcounter/perf_writer_disk.c
blob: 15188d253141fa7cec8a201a777ad7c80915349f (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* 
 *  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, see <http://www.gnu.org/licenses/>.
 */

#include "perf.h"

void init_diskdata_desc(PERF_DATA_BLOCK *data)
{
	init_perf_counter(&(data->diskInfo.diskObjDesc),
			  &(data->diskInfo.diskObjDesc),
			  get_counter_id(data),
			  "Logical Disk",
			  "The Logical Disk object consists of counters that show information about disks.",
			  0,
			  PERF_OBJECT);
	init_perf_counter(&(data->diskInfo.freeMegs),
			  &(data->diskInfo.diskObjDesc),
			  get_counter_id(data),
			  "Megabytes Free",
			  "The amount of available disk space, in megabytes.",
			  PERF_SIZE_LARGE | PERF_TYPE_NUMBER | PERF_NUMBER_DECIMAL | PERF_DISPLAY_NO_SUFFIX,
			  PERF_COUNTER);
	init_perf_counter(&(data->diskInfo.writesPerSec),
			  &(data->diskInfo.diskObjDesc),
			  get_counter_id(data),
			  "Writes/sec",
			  "The number of writes per second to that disk.",
			  PERF_SIZE_DWORD | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_DELTA_COUNTER | PERF_DISPLAY_PER_SEC,
			  PERF_COUNTER);
	init_perf_counter(&(data->diskInfo.readsPerSec),
			  &(data->diskInfo.diskObjDesc),
			  get_counter_id(data),
			  "Reads/sec",
			  "The number of reads of that disk per second.",
			  PERF_SIZE_DWORD | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_DELTA_COUNTER | PERF_DISPLAY_PER_SEC,
			  PERF_COUNTER);

	return;
}
void init_num_disks(PERF_DATA_BLOCK *data)
{
	FILE *mtab;
	char buf[PROC_BUF];
	char *start, *stop;
	int i = 0, num;

	if(!(mtab = fopen("/etc/mtab", "r")))
	{
		perror("init_disk_names: fopen");
		exit(1);
	}

	rewind(mtab);
	fflush(mtab);

	while(fgets(buf, sizeof(buf), mtab))
	{
		if(start = strstr(buf, "/dev/"))
		{
			if(start = strstr(start, "da"))
			{
				i++;
			}
		}
	}

	data->diskInfo.numDisks = i;
	fclose(mtab);

	return;
}
	
void init_disk_names(PERF_DATA_BLOCK *data)
{
	FILE *mtab;
	char buf[PROC_BUF];
	char *start, *stop;
	int i = 0, num;

	if(!(mtab = fopen("/etc/mtab", "r")))
	{
		perror("init_disk_names: fopen");
		exit(1);
	}

	rewind(mtab);
	fflush(mtab);

	while(fgets(buf, sizeof(buf), mtab))
	{
		if(start = strstr(buf, "/dev/"))
		{
			if(start = strstr(start, "da"))
			{
				start -=1;
				stop = strstr(start, " ");
				memcpy(data->diskInfo.mdata[i].name, start, stop - start);
				start = stop +1;
				stop = strstr(start, " ");
				memcpy(data->diskInfo.mdata[i].mountpoint, start, stop - start);
				i++;
			}
		}
	}

	fclose(mtab);

	return;
}

void get_diskinfo(PERF_DATA_BLOCK *data)
{
	int i;
	DiskData *p;
	struct statfs statfsbuf;
	int status, num;
	char buf[LARGE_BUF], *start;
	FILE *diskstats;
	long reads, writes, discard;

	diskstats = fopen("/proc/diskstats", "r");
	rewind(diskstats);
	fflush(diskstats);
	status = fread(buf, sizeof(char), LARGE_BUF, diskstats);
	fclose(diskstats);

	for(i = 0; i < data->diskInfo.numDisks; i++)
	{
		p = &(data->diskInfo.data[i]);
		status = statfs(data->diskInfo.mdata[i].mountpoint, &statfsbuf);
		p->freeMegs = (statfsbuf.f_bfree*statfsbuf.f_bsize)/1048576;
		start = strstr(buf, data->diskInfo.mdata[i].name);
		start += strlen(data->diskInfo.mdata[i].name) + 1;
		num = sscanf(start, "%u %u %u %u",
			     &reads,
			     &discard, 
			     &writes, 
			     &discard);
		p->writesPerSec = writes;
		p->readsPerSec = reads;
		fprintf(stderr, "%s:\t%u\t%u\n",
			data->diskInfo.mdata[i].mountpoint,
			reads, writes);
	}
	return;
}
void init_disk_data(PERF_DATA_BLOCK *data)
{
	init_diskdata_desc(data);
	
	init_num_disks(data);

	data->diskInfo.mdata = calloc(data->diskInfo.numDisks, sizeof(DiskMetaData));
	if(!data->diskInfo.mdata)
	{
		fatal("init_disk_data: out of memory");
	}

	init_disk_names(data);

	data->diskInfo.data = calloc(data->diskInfo.numDisks, sizeof(DiskData));
	if(!data->diskInfo.data)
	{
		fatal("init_disk_data: out of memory");
	}

	get_diskinfo(data);

	return;
}

void output_disk_desc(PERF_DATA_BLOCK *data, RuntimeSettings rt)
{
	output_perf_desc(data->diskInfo.diskObjDesc, rt);
	output_perf_desc(data->diskInfo.freeMegs, rt);
	output_perf_desc(data->diskInfo.writesPerSec, rt);
	output_perf_desc(data->diskInfo.readsPerSec, rt);
	output_num_instances(data->diskInfo.diskObjDesc, data->diskInfo.numDisks, rt);

	return;
}

void output_diskinfo(PERF_DATA_BLOCK *data, RuntimeSettings rt, int tdb_flags)
{
	int i;
	
	output_perf_counter(data->diskInfo.freeMegs,
			    data->diskInfo.data[0].freeMegs,
			    rt, tdb_flags);
	output_perf_counter(data->diskInfo.writesPerSec,
			    (unsigned long long)data->diskInfo.data[0].writesPerSec,
			    rt, tdb_flags);
	output_perf_counter(data->diskInfo.readsPerSec,
			    (unsigned long long)data->diskInfo.data[0].readsPerSec,
			    rt, tdb_flags);

	for(i = 0; i < data->diskInfo.numDisks; i++)
	{
		output_perf_instance(data->diskInfo.diskObjDesc.index,
				     i,
				     (void *)&(data->diskInfo.data[i]),
				     sizeof(DiskData),
				     data->diskInfo.mdata[i].mountpoint,
				     rt, tdb_flags);
	}

	return;
}