Linux CPU utilization: Difference between revisions

Added Wren
(Added Wren)
Line 1,059:
out "\r" console
end while</lang>
 
=={{header|Wren}}==
For some reason (probably because it's a ''virtual'' file) Wren-cli is unable to read /proc/stat properly. We therefore need to fall back on an embedded program with the C host getting the first line of the file for us so we can analyze it.
 
The figure at time 0 is the cumulative CPU usage since boot time.
<lang ecmascript>import "./fmt" for Fmt
 
class C {
// get the first line from /proc/stat
foreign static procStat1
 
foreign static sleep(secs)
}
 
var prevIdleTime = 0
var prevTotalTime = 0
System.print("CPU usage \% at 1 second intervals:\n")
for (i in 0..9) {
var line = C.procStat1[3..-1].trim()
var times = line.split(" ")[0..7].map { |t| Num.fromString(t) }.toList
var totalTime = times.reduce { |acc, t| acc + t }
var idleTime = times[3]
var deltaIdleTime = idleTime - prevIdleTime
var deltaTotalTime = totalTime - prevTotalTime
var cpuUsage = (1 - deltaIdleTime/deltaTotalTime) * 100
Fmt.print("$d : $6.3f", i, cpuUsage)
prevIdleTime = idleTime
prevTotalTime = totalTime
C.sleep(1)
}</lang>
<br>
We now embed this in the following C program, compile and run it.
<lang c>/* gcc linux_cpu_utilization.c -o linux_cpu_utilization -lwren -lm */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "wren.h"
 
/* C <=> Wren interface functions */
 
void C_procStat1(WrenVM* vm) {
FILE* fp = fopen("/proc/stat","r");
char str[100];
fgets(str, 100, fp);
fclose(fp);
wrenSetSlotString(vm, 0, str);
}
 
void C_sleep(WrenVM* vm) {
int seconds = (int)wrenGetSlotDouble(vm, 1);
sleep(seconds);
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "procStat1") == 0) return C_procStat1;
if (isStatic && strcmp(signature, "sleep(_)") == 0) return C_sleep;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
static void loadModuleComplete(WrenVM* vm, const char* module, WrenLoadModuleResult result) {
if( result.source) free((void*)result.source);
}
 
WrenLoadModuleResult loadModule(WrenVM* vm, const char* name) {
WrenLoadModuleResult result = {0};
if (strcmp(name, "random") != 0 && strcmp(name, "meta") != 0) {
result.onComplete = loadModuleComplete;
char fullName[strlen(name) + 6];
strcpy(fullName, name);
strcat(fullName, ".wren");
result.source = readFile(fullName);
}
return result;
}
 
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignMethodFn = &bindForeignMethod;
config.loadModuleFn = &loadModule;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "linux_cpu_utilization.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</lang>
 
{{out}}
Sample output:
<pre>
CPU usage % at 1 second intervals:
 
0 : 3.018
1 : 1.378
2 : 1.748
3 : 1.129
4 : 0.755
5 : 1.746
6 : 2.114
7 : 1.004
8 : 1.375
9 : 1.253
</pre>
 
=={{header|zkl}}==
9,479

edits