aboutsummaryrefslogtreecommitdiff
path: root/tools/gator/daemon/DiskIODriver.cpp
blob: af62bb9a95bd27393812af43d9be0755490f9bc2 (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
/**
 * Copyright (C) ARM Limited 2013-2015. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

// Define to get format macros from inttypes.h
#define __STDC_FORMAT_MACROS

#include "DiskIODriver.h"

#include <inttypes.h>

#include "Logging.h"
#include "SessionData.h"

class DiskIOCounter : public DriverCounter {
public:
	DiskIOCounter(DriverCounter *next, char *const name, int64_t *const value);
	~DiskIOCounter();

	int64_t read();

private:
	int64_t *const mValue;
	int64_t mPrev;

	// Intentionally unimplemented
	DiskIOCounter(const DiskIOCounter &);
	DiskIOCounter &operator=(const DiskIOCounter &);
};

DiskIOCounter::DiskIOCounter(DriverCounter *next, char *const name, int64_t *const value) : DriverCounter(next, name), mValue(value), mPrev(0) {
}

DiskIOCounter::~DiskIOCounter() {
}

int64_t DiskIOCounter::read() {
	int64_t result = *mValue - mPrev;
	mPrev = *mValue;
	// Kernel assumes a sector is 512 bytes
	return result << 9;
}

DiskIODriver::DiskIODriver() : mBuf(), mReadBytes(0), mWriteBytes(0) {
}

DiskIODriver::~DiskIODriver() {
}

void DiskIODriver::readEvents(mxml_node_t *const) {
	// Only for use with perf
	if (!gSessionData->perf.isSetup()) {
		return;
	}

	setCounters(new DiskIOCounter(getCounters(), strdup("Linux_block_rq_rd"), &mReadBytes));
	setCounters(new DiskIOCounter(getCounters(), strdup("Linux_block_rq_wr"), &mWriteBytes));
}

void DiskIODriver::doRead() {
	if (!countersEnabled()) {
		return;
	}

	if (!mBuf.read("/proc/diskstats")) {
		logg->logError("Unable to read /proc/diskstats");
		handleException();
	}

	mReadBytes = 0;
	mWriteBytes = 0;

	char *lastName = NULL;
	int lastNameLen = -1;
	char *line = mBuf.getBuf();
	while (*line != '\0') {
		char *end = strchr(line, '\n');
		if (end != NULL) {
			*end = '\0';
		}

		int nameStart = -1;
		int nameEnd = -1;
		int64_t readBytes = -1;
		int64_t writeBytes = -1;
		const int count = sscanf(line, "%*d %*d %n%*s%n %*u %*u %" SCNu64 " %*u %*u %*u %" SCNu64, &nameStart, &nameEnd, &readBytes, &writeBytes);
		if (count != 2) {
			logg->logError("Unable to parse /proc/diskstats");
			handleException();
		}

		// Skip partitions which are identified if the name is a substring of the last non-partition
		if ((lastName == NULL) || (strncmp(lastName, line + nameStart, lastNameLen) != 0)) {
			lastName = line + nameStart;
			lastNameLen = nameEnd - nameStart;
			mReadBytes += readBytes;
			mWriteBytes += writeBytes;
		}

		if (end == NULL) {
			break;
		}
		line = end + 1;
	}
}

void DiskIODriver::start() {
	doRead();
	// Initialize previous values
	for (DriverCounter *counter = getCounters(); counter != NULL; counter = counter->getNext()) {
		if (!counter->isEnabled()) {
			continue;
		}
		counter->read();
	}
}

void DiskIODriver::read(Buffer *const buffer) {
	doRead();
	super::read(buffer);
}