コード例 #1
0
ファイル: DiskIODriver.cpp プロジェクト: ARM-software/gator
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;
        uint64_t readBytes = -1;
        uint64_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;
    }
}
コード例 #2
0
ファイル: NetDriver.cpp プロジェクト: commshare/gator
bool NetDriver::doRead() {
	if (!countersEnabled()) {
		return true;
	}

	if (!mBuf.read("/proc/net/dev")) {
		return false;
	}

	// Skip the header
	char *key;
	if (((key = strchr(mBuf.getBuf(), '\n')) == NULL) ||
			((key = strchr(key + 1, '\n')) == NULL)) {
		return false;
	}
	key = key + 1;

	mReceiveBytes = 0;
	mTransmitBytes = 0;

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

		int64_t receiveBytes;
		int64_t transmitBytes;
		const int count = sscanf(colon + 1, " %" SCNu64 " %*u %*u %*u %*u %*u %*u %*u %" SCNu64, &receiveBytes, &transmitBytes);
		if (count != 2) {
			return false;
		}
		mReceiveBytes += receiveBytes;
		mTransmitBytes += transmitBytes;

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

	return true;
}
コード例 #3
0
ファイル: MemInfoDriver.cpp プロジェクト: bigzz/gator
void MemInfoDriver::read(Buffer *const buffer) {
	if (!countersEnabled()) {
		return;
	}

	if (!mBuf.read("/proc/meminfo")) {
		logg.logError("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);
}