summaryrefslogtreecommitdiff
path: root/tools/gator/daemon/Driver.cpp
blob: a88122a3114a20d9aa8a6d039a05948058ab0877 (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
/**
 * Copyright (C) ARM Limited 2013-2016. 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.
 */

#include "Driver.h"

#include "Buffer.h"
#include "SessionData.h"

DriverCounter::DriverCounter(DriverCounter *const next, const char *const name) : mNext(next), mName(name), mKey(getEventKey()), mEnabled(false) {
}

DriverCounter::~DriverCounter() {
	delete mName;
}

Driver *Driver::head = NULL;

Driver::Driver() : next(head) {
	head = this;
}

SimpleDriver::~SimpleDriver() {
	DriverCounter *counters = mCounters;
	while (counters != NULL) {
		DriverCounter *counter = counters;
		counters = counter->getNext();
		delete counter;
	}
}

DriverCounter *SimpleDriver::findCounter(const Counter &counter) const {
	for (DriverCounter *driverCounter = mCounters; driverCounter != NULL; driverCounter = driverCounter->getNext()) {
		if (strcmp(driverCounter->getName(), counter.getType()) == 0) {
			return driverCounter;
		}
	}

	return NULL;
}

Driver::~Driver() {
}

bool SimpleDriver::claimCounter(const Counter &counter) const {
	return findCounter(counter) != NULL;
}

bool SimpleDriver::countersEnabled() const {
	for (DriverCounter *counter = mCounters; counter != NULL; counter = counter->getNext()) {
		if (counter->isEnabled()) {
			return true;
		}
	}
	return false;
}

void SimpleDriver::resetCounters() {
	for (DriverCounter *counter = mCounters; counter != NULL; counter = counter->getNext()) {
		counter->setEnabled(false);
	}
}

void SimpleDriver::setupCounter(Counter &counter) {
	DriverCounter *const driverCounter = findCounter(counter);
	if (driverCounter == NULL) {
		counter.setEnabled(false);
		return;
	}
	driverCounter->setEnabled(true);
	counter.setKey(driverCounter->getKey());
}

int SimpleDriver::writeCounters(mxml_node_t *root) const {
	int count = 0;
	for (DriverCounter *counter = mCounters; counter != NULL; counter = counter->getNext()) {
		mxml_node_t *node = mxmlNewElement(root, "counter");
		mxmlElementSetAttr(node, "name", counter->getName());
		++count;
	}

	return count;
}

PolledDriver::~PolledDriver() {
}

void PolledDriver::read(Buffer *const buffer) {
	for (DriverCounter *counter = getCounters(); counter != NULL; counter = counter->getNext()) {
		if (!counter->isEnabled()) {
			continue;
		}
		buffer->event64(counter->getKey(), counter->read());
	}
}