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
|
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
/* misc. */
#include "m_pd.h"
#include "s_stuff.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <wtypes.h>
#include <time.h>
#else
#include <sys/types.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/param.h>
#include <unistd.h>
#endif /* _WIN32 */
#if defined (__APPLE__) || defined (__FreeBSD__)
#define CLOCKHZ CLK_TCK
#endif
#if defined (__linux__) || defined (__CYGWIN__) || defined (ANDROID)
#define CLOCKHZ sysconf(_SC_CLK_TCK)
#endif
#if defined (__FreeBSD_kernel__) || defined(__GNU__)
#include <time.h>
#define CLOCKHZ CLOCKS_PER_SEC
#endif
static t_class *cputime_class;
typedef struct _cputime
{
t_object x_obj;
#ifdef _WIN32
LARGE_INTEGER x_kerneltime;
LARGE_INTEGER x_usertime;
int x_warned;
#else
struct tms x_setcputime;
#endif /* _WIN32 */
} t_cputime;
static void cputime_bang(t_cputime *x)
{
#ifdef _WIN32
FILETIME ignorethis, ignorethat;
BOOL retval;
retval = GetProcessTimes(GetCurrentProcess(), &ignorethis, &ignorethat,
(FILETIME *)&x->x_kerneltime, (FILETIME *)&x->x_usertime);
if (!retval)
{
if (!x->x_warned)
post("cputime is apparently not supported on your platform");
x->x_warned = 1;
x->x_kerneltime.QuadPart = 0;
x->x_usertime.QuadPart = 0;
}
#else
times(&x->x_setcputime);
#endif /* _WIN32 */
}
static void cputime_bang2(t_cputime *x)
{
#ifndef _WIN32
t_float elapsedcpu;
struct tms newcputime;
times(&newcputime);
elapsedcpu = 1000 * (
newcputime.tms_utime + newcputime.tms_stime -
x->x_setcputime.tms_utime - x->x_setcputime.tms_stime) / CLOCKHZ;
outlet_float(x->x_obj.ob_outlet, elapsedcpu);
#else
t_float elapsedcpu;
FILETIME ignorethis, ignorethat;
LARGE_INTEGER usertime, kerneltime;
BOOL retval;
retval = GetProcessTimes(GetCurrentProcess(), &ignorethis, &ignorethat,
(FILETIME *)&kerneltime, (FILETIME *)&usertime);
if (retval)
elapsedcpu = 0.0001 *
((kerneltime.QuadPart - x->x_kerneltime.QuadPart) +
(usertime.QuadPart - x->x_usertime.QuadPart));
else elapsedcpu = 0;
outlet_float(x->x_obj.ob_outlet, elapsedcpu);
#endif /* NOT _WIN32 */
}
static void *cputime_new(void)
{
t_cputime *x = (t_cputime *)pd_new(cputime_class);
outlet_new(&x->x_obj, gensym("float"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("bang"), gensym("bang2"));
#ifdef _WIN32
x->x_warned = 0;
#endif
cputime_bang(x);
return (x);
}
void cputime_setup(void)
{
cputime_class = class_new(gensym("cputime"), (t_newmethod)cputime_new, 0,
sizeof(t_cputime), 0, 0);
class_addbang(cputime_class, cputime_bang);
class_addmethod(cputime_class, (t_method)cputime_bang2, gensym("bang2"), 0);
}
|