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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/* cpupercent
*
* Copyright *c) 2011 John ffitch, based heavily on code:
* Copyright (c) 2002, by: James C. Warner
* All rights reserved. 8921 Hilloway Road
* Eden Prairie, Minnesota 55347 USA
* <warnerjc@worldnet.att.net>
*
* This file may be used subject to the terms and conditions of the
* GNU Library General Public License Version 2, or any later version
* at your option, as published by the Free Software Foundation.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
*/
#ifndef WIN32
#include "csoundCore.h"
#include <time.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <float.h>
// only available on Linux (no /proc/stat on OSX)
#if defined(LINUX)
/*###### Miscellaneous global stuff ####################################*/
#define SMLBUFSIZ (512)
#define TEST (0)
typedef unsigned long long TIC_t;
typedef long long SIC_t;
typedef struct CPU_t {
TIC_t u, n, s, i, w, x, y, z; // as represented in /proc/stat
TIC_t u_sav, n_sav, s_sav, i_sav,
w_sav, x_sav, y_sav, z_sav;
unsigned id; // the CPU ID number
} CPU_t;
typedef struct {
OPDS h;
MYFLT *k0, *kk[8], *itrig;
AUXCH cpu_a;
CPU_t *cpus;
uint32_t cpu_max;
int32_t cnt, trig;
FILE *fp;
} CPUMETER;
/*
* we preserve all cpu data in our CPU_t array which is organized
* as follows:
* cpus[0] thru cpus[n] == tics for each separate cpu
* cpus[Cpu_tot] == tics from the 1st /proc/stat line */
static int32_t deinit_cpupercent(CSOUND *csound, void *pdata)
{
CPUMETER *p = (CPUMETER *) pdata;
fclose(p->fp);
return OK;
}
static int32_t cpupercent_renew(CSOUND *csound, CPUMETER* p);
int32_t cpupercent_init(CSOUND *csound, CPUMETER* p)
{
char buf[SMLBUFSIZ];
int32_t k, num;
TIC_t id, u, n, s, i, w, x, y, z;
if (!(p->fp = fopen("/proc/stat", "r")))
return
csound->InitError(csound,
Str("Failed to open /proc/stat: %s"), strerror(errno));
if (!fgets(buf, sizeof(buf), p->fp))
return csound->InitError(csound, Str("failed /proc/stat read"));
num = sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
&u, &n, &s, &i, &w, &x, &y, &z);
for (k = 0; ; k++) {
if (!fgets(buf, SMLBUFSIZ, p->fp))
return csound->InitError(csound,Str("failed /proc/stat read"));
num = sscanf(buf, "cpu%llu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
&id, &u, &n, &s, &i, &w, &x, &y, &z);
if (num<4) break;
}
p->cpu_max = k-1;
csound->AuxAlloc(csound,k*sizeof(CPU_t), &(p->cpu_a));
p->cpus = (CPU_t *) p->cpu_a.auxp;
k = cpupercent_renew(csound, p);
p->cnt = (p->trig = (int32_t)(*p->itrig * csound->GetSr(csound)));
// Would it be better to add a deinit process so the closing happens in perf?
//csound->CreateFileHandle(csound, &p->fp, CSFILE_STD, "/proc/stat");
csound->RegisterDeinitCallback(csound, (void *) p, deinit_cpupercent);
return k;
}
static int32_t cpupercent_renew(CSOUND *csound, CPUMETER* p)
{
#define TRIMz(x) ((tz = (SIC_t)(x)) < 0 ? 0 : tz)
SIC_t u_frme, s_frme, n_frme, i_frme,
w_frme, x_frme, y_frme, z_frme, tot_frme, tz;
double scale;
uint32_t k;
CPU_t *cpu = p->cpus;
char buf[SMLBUFSIZ];
rewind(p->fp);
fflush(p->fp);
k = p->cpu_max;
if (!fgets(buf, SMLBUFSIZ, p->fp))
return csound->PerfError(csound, &(p->h),
Str("failed /proc/stat read"));
/*num = */sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
&cpu[k].u, &cpu[k].n, &cpu[k].s, &cpu[k].i,
&cpu[k].w, &cpu[k].x, &cpu[k].y, &cpu[k].z);
u_frme = cpu[k].u - cpu[k].u_sav;
s_frme = cpu[k].s - cpu[k].s_sav;
n_frme = cpu[k].n - cpu[k].n_sav;
i_frme = TRIMz(cpu[k].i - cpu[k].i_sav);
w_frme = cpu[k].w - cpu[k].w_sav;
x_frme = cpu[k].x - cpu[k].x_sav;
y_frme = cpu[k].y - cpu[k].y_sav;
z_frme = cpu[k].z - cpu[k].z_sav;
tot_frme = u_frme + s_frme + n_frme + i_frme +
w_frme + x_frme + y_frme + z_frme;
if (tot_frme < 1) tot_frme = 1;
scale = 100.0 / (double)tot_frme;
*p->k0 = 100.0-(double)i_frme * scale;
if (TEST)
printf("**%5.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
(double)u_frme * scale,
(double)s_frme * scale,
(double)n_frme * scale,
(double)i_frme * scale,
(double)w_frme * scale,
(double)x_frme * scale,
(double)y_frme * scale,
(double)z_frme * scale
);
// remember for next time around
cpu[k].u_sav = cpu[k].u;
cpu[k].s_sav = cpu[k].s;
cpu[k].n_sav = cpu[k].n;
cpu[k].i_sav = cpu[k].i;
cpu[k].w_sav = cpu[k].w;
cpu[k].x_sav = cpu[k].x;
cpu[k].y_sav = cpu[k].y;
cpu[k].z_sav = cpu[k].z;
for (k=0; k<p->cpu_max && k+1<p->OUTOCOUNT; k++) {
if (!fgets(buf, SMLBUFSIZ, p->fp))
return csound->PerfError(csound, &(p->h),
Str("failed /proc/stat read"));
/*num = */ (void)sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
&cpu[k].u, &cpu[k].n, &cpu[k].s, &cpu[k].i,
&cpu[k].w, &cpu[k].x, &cpu[k].y, &cpu[k].z);
u_frme = cpu[k].u - cpu[k].u_sav;
s_frme = cpu[k].s - cpu[k].s_sav;
n_frme = cpu[k].n - cpu[k].n_sav;
i_frme = TRIMz(cpu[k].i - cpu[k].i_sav);
w_frme = cpu[k].w - cpu[k].w_sav;
x_frme = cpu[k].x - cpu[k].x_sav;
y_frme = cpu[k].y - cpu[k].y_sav;
z_frme = cpu[k].z - cpu[k].z_sav;
tot_frme = u_frme + s_frme + n_frme + i_frme +
w_frme + x_frme + y_frme + z_frme;
if (tot_frme < 1) tot_frme = 1;
scale = 100.0 / (double)tot_frme;
//if (p->kk[k]==NULL) break;
*p->kk[k] = 100.0-i_frme * scale;
if (TEST)
printf("%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
(double)u_frme * scale,
(double)s_frme * scale,
(double)n_frme * scale,
(double)i_frme * scale,
(double)w_frme * scale,
(double)x_frme * scale,
(double)y_frme * scale,
(double)z_frme * scale);
// remember for next time around
cpu[k].u_sav = cpu[k].u;
cpu[k].s_sav = cpu[k].s;
cpu[k].n_sav = cpu[k].n;
cpu[k].i_sav = cpu[k].i;
cpu[k].w_sav = cpu[k].w;
cpu[k].x_sav = cpu[k].x;
cpu[k].y_sav = cpu[k].y;
cpu[k].z_sav = cpu[k].z;
*p->kk[k] = 100.0-i_frme * scale;
}
return OK;
#undef TRIMz
}
static int32_t cpupercent(CSOUND *csound, CPUMETER* p)
{
p->cnt -= CS_KSMPS;
if (p->cnt< 0) {
int32_t n = cpupercent_renew(csound, p);
p->cnt = p->trig;
return n;
}
return OK;
}
#else
typedef struct {
OPDS h;
MYFLT *k0, *kk[8], *itrig;
} CPUMETER;
int32_t cpupercent_init(CSOUND *csound, CPUMETER *p) {
IGN(p);
csound->Message(csound, "not implemented\n");
return OK;
}
int32_t cpupercent(CSOUND *c, CPUMETER *p) {
IGN(c);
IGN(p);
return OK;
}
#endif
typedef struct {
OPDS h;
MYFLT *ti;
} SYST;
static int32_t
systime(CSOUND *csound, SYST *p){
IGN(csound);
#if HAVE_CLOCK_GETTIME
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
*p->ti = round((ts.tv_sec + ts.tv_nsec/1.e9)*1000);
#else
*p->ti = FL(0.0);
#endif
return OK;
}
#define S(x) sizeof(x)
static OENTRY cpumeter_localops[] = {
{ "cpumeter", S(CPUMETER), 0,3, "kzzzzzzzz", "i",
(SUBR)cpupercent_init, (SUBR)cpupercent },
{ "systime", S(SYST),0, 3, "k", "", (SUBR)systime, (SUBR)systime},
{ "systime", S(SYST),0, 1, "i", "", (SUBR)systime}
};
LINKAGE_BUILTIN(cpumeter_localops)
#endif
|