Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug in sigar_disk_usage_get and change counter index #61

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 81 additions & 13 deletions src/os/win32/win32_sigar.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define PERF_TITLE_PROC 230
#define PERF_TITLE_SYS_KEY "2"
#define PERF_TITLE_MEM_KEY "4"
#define PERF_TITLE_PAGING_FILE_KEY "700"
#define PERF_TITLE_PROC_KEY "230"
#define PERF_TITLE_CPU_KEY "238"
#define PERF_TITLE_DISK_KEY "236"
Expand Down Expand Up @@ -93,9 +94,9 @@ typedef enum {
PERF_IX_DISK_MAX
} perf_disk_offsets_t;

#define PERF_TITLE_DISK_TIME 200 /* % Disk Time */
#define PERF_TITLE_DISK_READ_TIME 202 /* % Disk Read Time */
#define PERF_TITLE_DISK_WRITE_TIME 204 /* % Disk Write Time */
#define PERF_TITLE_DISK_TIME 1400 /* % Disk Time */
#define PERF_TITLE_DISK_READ_TIME 1402 /* % Disk Read Time */
#define PERF_TITLE_DISK_WRITE_TIME 1404 /* % Disk Write Time */
#define PERF_TITLE_DISK_READ 214 /* Disk Reads/sec */
#define PERF_TITLE_DISK_WRITE 216 /* Disk Writes/sec */
#define PERF_TITLE_DISK_READ_BYTES 220 /* Disk Read Bytes/sec */
Expand All @@ -122,6 +123,11 @@ typedef enum {
perf_offsets[ix] ? \
*((DWORD *)((BYTE *)counter_block + perf_offsets[ix])) : 0

#define PERF_VAL_LARGE(ix) \
perf_offsets[ix] ? \
*((ULONGLONG *)((BYTE *)counter_block + perf_offsets[ix])) : 0


/* 1/100ns units to milliseconds */
#define NS100_2MSEC(t) ((t) / 10000)

Expand Down Expand Up @@ -289,6 +295,67 @@ static int get_mem_counters(sigar_t *sigar, sigar_swap_t *swap, sigar_mem_t *mem
return SIGAR_OK;
}

static int get_swap_usage(sigar_t *sigar, sigar_swap_t *swap)
{
int status;
PERF_OBJECT_TYPE *object =
get_perf_object_inst(sigar, PERF_TITLE_PAGING_FILE_KEY, 0, &status);
PERF_INSTANCE_DEFINITION *inst;
PERF_COUNTER_DEFINITION *counter;
PERF_COUNTER_BLOCK *block;
char name[MAX_PATH];
char *p;
BYTE *data;
DWORD i;

if (!object) {
return status;
}

inst = (PERF_INSTANCE_DEFINITION *)((BYTE *)object + object->DefinitionLength);

for (i = 0; i < object->NumInstances; i++) {
p = (char *)(((char *)inst) + inst->NameOffset);

SIGAR_W2A(p, name, sizeof(name));

if (stricmp(name, "_Total") == 0) {
break;
}

block = (PERF_COUNTER_BLOCK*)((LPBYTE)inst + inst->ByteLength);
inst = (PERF_INSTANCE_DEFINITION*)((LPBYTE)block + block->ByteLength);
}

block = (PERF_COUNTER_BLOCK*)((LPBYTE)inst + inst->ByteLength);


for (i=0, counter = PdhFirstCounter(object);
i<object->NumCounters;
i++, counter = PdhNextCounter(counter))
{
DWORD offset = counter->CounterOffset;

switch (counter->CounterNameTitleIndex) {
case 702:
data = (LPBYTE)block;

swap->used = (sigar_uint64_t)(swap->total * (
(*(DWORD *)(data + offset)) /
(float)(*(DWORD *)(data + (counter+1)->CounterOffset))));
swap->free = swap->total - swap->used;

return SIGAR_OK;
default:
continue;
}
}

/* unreachable */

return SIGAR_OK;
}

static void get_sysinfo(sigar_t *sigar)
{
SYSTEM_INFO sysinfo;
Expand Down Expand Up @@ -713,17 +780,18 @@ SIGAR_DECLARE(int) sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap)
return GetLastError();
}

swap->total = memstat.ullTotalPageFile;
swap->free = memstat.ullAvailPageFile;
swap->total = memstat.ullTotalPageFile - memstat.ullTotalPhys;
}
else {
MEMORYSTATUS memstat;
GlobalMemoryStatus(&memstat);
swap->total = memstat.dwTotalPageFile;
swap->free = memstat.dwAvailPageFile;
swap->total = memstat.dwTotalPageFile - memstat.dwTotalPhys;
}

swap->used = swap->total - swap->free;
status = get_swap_usage(sigar, swap);
if (status != SIGAR_OK) {
return status;
}

if (get_mem_counters(sigar, swap, NULL) != SIGAR_OK) {
swap->page_in = SIGAR_FIELD_NOTIMPL;
Expand Down Expand Up @@ -2056,13 +2124,13 @@ SIGAR_DECLARE(int) sigar_disk_usage_get(sigar_t *sigar,
}

if (strnEQ(drive, dirname, 2)) {
disk->time = PERF_VAL(PERF_IX_DISK_TIME);
disk->rtime = PERF_VAL(PERF_IX_DISK_READ_TIME);
disk->wtime = PERF_VAL(PERF_IX_DISK_WRITE_TIME);
disk->time = PERF_VAL_LARGE(PERF_IX_DISK_TIME);
disk->rtime = PERF_VAL_LARGE(PERF_IX_DISK_READ_TIME);
disk->wtime = PERF_VAL_LARGE(PERF_IX_DISK_WRITE_TIME);
disk->reads = PERF_VAL(PERF_IX_DISK_READ);
disk->writes = PERF_VAL(PERF_IX_DISK_WRITE);
disk->read_bytes = PERF_VAL(PERF_IX_DISK_READ_BYTES);
disk->write_bytes = PERF_VAL(PERF_IX_DISK_WRITE_BYTES);
disk->read_bytes = PERF_VAL_LARGE(PERF_IX_DISK_READ_BYTES);
disk->write_bytes = PERF_VAL_LARGE(PERF_IX_DISK_WRITE_BYTES);
disk->queue = PERF_VAL(PERF_IX_DISK_QUEUE);
return SIGAR_OK;
}
Expand Down