-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
10,064 additions
and
299 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[Unit] | ||
Description=Cluster-Smi-Node | ||
|
||
After=network.target local-fs.target multi-user.target | ||
Requires=network.target local-fs.target multi-user.target | ||
|
||
|
||
[Service] | ||
Type=simple | ||
StandardOutput=journal+console | ||
ExecStart=/path/to/cluster-smi-node | ||
|
||
## Add me by: sudo systemctl enable cluster-smi-node.service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
Code in this directory is borrowed from: | ||
The original code in this directory is borrowed from: | ||
https://github.com/tankbusta/nvidia_exporter | ||
|
||
I modified some parts. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Author: Patrick Wieschollek, 2018 | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define MAX_NAME 128 | ||
|
||
void get_mem(unsigned long *mem_total, unsigned long *mem_free, unsigned long *mem_available) { | ||
char line[100], *p; | ||
FILE* statusf; | ||
|
||
|
||
statusf = fopen("/proc/meminfo", "r"); | ||
if (!statusf) | ||
return; | ||
|
||
|
||
fgets(line, 100, statusf); | ||
sscanf(line, "%*s %lu %*s", mem_total); | ||
fgets(line, 100, statusf); | ||
sscanf(line, "%*s %lu %*s", mem_free); | ||
fgets(line, 100, statusf); | ||
sscanf(line, "%*s %lu %*s", mem_available); | ||
|
||
|
||
fclose(statusf); | ||
} | ||
|
||
// read total cpu time | ||
unsigned long long int read_cpu_tick() { | ||
unsigned long long int usertime, nicetime, systemtime, idletime; | ||
unsigned long long int ioWait, irq, softIrq, steal, guest, guestnice; | ||
usertime = nicetime = systemtime = idletime = 0; | ||
ioWait = irq = softIrq = steal = guest = guestnice = 0; | ||
|
||
FILE *fp; | ||
fp = fopen("/proc/stat", "r"); | ||
if (fp != NULL) { | ||
if (fscanf(fp, "cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", | ||
&usertime, &nicetime, &systemtime, &idletime, | ||
&ioWait, &irq, &softIrq, &steal, &guest, &guestnice) == EOF) { | ||
fclose(fp); | ||
return 0; | ||
} else { | ||
fclose(fp); | ||
return usertime + nicetime + systemtime + idletime + ioWait + irq + softIrq + steal + guest + guestnice; | ||
} | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
void get_uid_from_pid(unsigned long pid, unsigned long *uid) { | ||
char path[40], line[100], *p; | ||
FILE* statusf; | ||
|
||
snprintf(path, 40, "/proc/%ld/status", pid); | ||
|
||
statusf = fopen(path, "r"); | ||
if (!statusf) | ||
return; | ||
|
||
while (fgets(line, 100, statusf)) { | ||
if (strncmp(line, "Uid:", 4) != 0) | ||
continue; | ||
// Uid: 1000 1000 1000 1000 | ||
sscanf(line, "%*s %lu %*s", uid); | ||
break; | ||
} | ||
fclose(statusf); | ||
} | ||
|
||
// read cpu tick for a specific process | ||
void read_time_and_name_from_pid(unsigned long pid, unsigned long *time, char *name) { | ||
|
||
char fn[MAX_NAME + 1]; | ||
snprintf(fn, sizeof fn, "/proc/%ld/stat", pid); | ||
|
||
unsigned long utime = 0; | ||
unsigned long stime = 0; | ||
|
||
*time = 0; | ||
|
||
FILE * fp; | ||
fp = fopen(fn, "r"); | ||
if (fp != NULL) { | ||
/* | ||
(1) pid %d The process ID | ||
(2) comm %s The filename of the executable, in parentheses. | ||
(3) state %c | ||
(4) ppid %d The PID of the parent of this process. | ||
(5) pgrp %d The process group ID of the process. | ||
(6) session %d The session ID of the process. | ||
(7) tty_nr %d The controlling terminal of the process. | ||
(8) tpgid %d The ID of the foreground process group | ||
(9) flags %u The kernel flags word of the process. | ||
(10) minflt %lu The number of minor faults the process has made | ||
(11) cminflt %lu The number of minor faults that the process's waited-for children have made. | ||
(12) majflt %lu The number of major faults the process has made | ||
(13) cmajflt %lu The number of major faults that the process's | ||
(14) utime %lu Amount of time that this process has been scheduled in user mode | ||
(15) stime %lu Amount of time that this process has been scheduled in kernel mode | ||
... | ||
*/ | ||
|
||
// extract | ||
bool success = fscanf(fp, "%*d (%s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %lu" | ||
"%lu %*ld %*ld %*d %*d %*d %*d %*u %*lu %*ld", | ||
name, &utime, &stime) != EOF; | ||
fclose(fp); | ||
|
||
if (!success) { | ||
// something went wrong | ||
return; | ||
} | ||
|
||
// remove ")" suffix | ||
if (strlen(name) > 2) { | ||
name[strlen(name) - 1] = 0; | ||
} | ||
|
||
*time = utime + stime; | ||
|
||
} | ||
} | ||
|
||
// return number of cores | ||
unsigned int num_cores() { | ||
return sysconf(_SC_NPROCESSORS_ONLN); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package proc | ||
|
||
// #include "proc.h" | ||
// #include <stdlib.h> | ||
import "C" | ||
import "unsafe" | ||
|
||
func mallocCStringBuffer(size uint) *C.char { | ||
buf := make([]byte, size) | ||
return C.CString(string(buf)) | ||
} | ||
|
||
// CpuTick returns the total number of Jiffies | ||
func CpuTick() (t int64) { | ||
return int64(C.read_cpu_tick()) | ||
} | ||
|
||
// TimeAndNameFromPID returns used time (int) and command (string) | ||
func TimeAndNameFromPID(pid int) (int64, string) { | ||
time := C.ulong(0) | ||
|
||
var c_dst *C.char = mallocCStringBuffer(128 + 1) | ||
defer C.free(unsafe.Pointer(c_dst)) | ||
|
||
C.read_time_and_name_from_pid(C.ulong(pid), &time, c_dst) | ||
return int64(time), C.GoString(c_dst) | ||
} | ||
|
||
// NumberCPUCores returns number of CPU cores | ||
func NumberCPUCores() (n int) { | ||
return int(C.num_cores()) | ||
} | ||
|
||
// Find user id (UID) for a given process id (PID) | ||
func UIDFromPID(pid int) (uid int) { | ||
c_uid := C.ulong(0) | ||
C.get_uid_from_pid(C.ulong(pid), &c_uid) | ||
return int(c_uid) | ||
} | ||
|
||
// get memory information of RAM | ||
func GetRAMMemoryInfo() (total int64, free int64, available int64) { | ||
c_total := C.ulong(0) | ||
c_free := C.ulong(0) | ||
c_available := C.ulong(0) | ||
|
||
C.get_mem(&c_total, &c_free, &c_available) | ||
|
||
return int64(c_total), int64(c_free), int64(c_available) | ||
} |
Oops, something went wrong.