MemInfoDriver.cpp 2.21 KB
Newer Older
Abhijith PA's avatar
Abhijith PA committed
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
/**
 * Copyright (C) ARM Limited 2013-2014. 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 "MemInfoDriver.h"

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

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

	int64_t read();

private:
	int64_t *const mValue;

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

MemInfoCounter::MemInfoCounter(DriverCounter *next, char *const name, int64_t *const value) : DriverCounter(next, name), mValue(value) {
}

MemInfoCounter::~MemInfoCounter() {
}

int64_t MemInfoCounter::read() {
	return *mValue;
}

MemInfoDriver::MemInfoDriver() : mBuf(), mMemUsed(0), mMemFree(0), mBuffers(0) {
}

MemInfoDriver::~MemInfoDriver() {
}

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

	setCounters(new MemInfoCounter(getCounters(), strdup("Linux_meminfo_memused2"), &mMemUsed));
	setCounters(new MemInfoCounter(getCounters(), strdup("Linux_meminfo_memfree"), &mMemFree));
	setCounters(new MemInfoCounter(getCounters(), strdup("Linux_meminfo_bufferram"), &mBuffers));
}

void MemInfoDriver::read(Buffer *const buffer) {
	if (!countersEnabled()) {
		return;
	}

	if (!mBuf.read("/proc/meminfo")) {
		logg->logError(__FILE__, __LINE__, "Failed to read /proc/meminfo");
		handleException();
	}

	char *key = mBuf.getBuf();
	char *colon;
	int64_t memTotal = 0;
	while ((colon = strchr(key, ':')) != NULL) {
		char *end = strchr(colon + 1, '\n');
		if (end != NULL) {
			*end = '\0';
		}
		*colon = '\0';

		if (strcmp(key, "MemTotal") == 0) {
			memTotal = strtoll(colon + 1, NULL, 10) << 10;
		} else if (strcmp(key, "MemFree") == 0) {
			mMemFree = strtoll(colon + 1, NULL, 10) << 10;
		} else if (strcmp(key, "Buffers") == 0) {
			mBuffers = strtoll(colon + 1, NULL, 10) << 10;
		}

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

	mMemUsed = memTotal - mMemFree;

	super::read(buffer);
}