forked from torch/torch7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.c
170 lines (152 loc) · 4.19 KB
/
Timer.c
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
#include "general.h"
#ifdef _MSC_VER
#include <time.h>
#else
#include <sys/time.h>
#include <sys/resource.h>
#endif
typedef struct _Timer
{
int isRunning;
double totalrealtime;
double totalusertime;
double totalsystime;
double startrealtime;
double startusertime;
double startsystime;
#ifdef _MSC_VER
time_t base_time;
#endif
} Timer;
static double torch_Timer_realtime()
{
#ifdef WIN32
time_t ltime;
time(<ime);
return (double)(ltime);
#else
struct timeval current;
gettimeofday(¤t, NULL);
return (current.tv_sec + current.tv_usec/1000000.0);
#endif
}
static double torch_Timer_usertime()
{
#ifdef WIN32
return torch_Timer_realtime();
#else
struct rusage current;
getrusage(RUSAGE_SELF, ¤t);
return (current.ru_utime.tv_sec + current.ru_utime.tv_usec/1000000.0);
#endif
}
static double torch_Timer_systime()
{
#ifdef WIN32
return 0;
#else
struct rusage current;
getrusage(RUSAGE_SELF, ¤t);
return (current.ru_stime.tv_sec + current.ru_stime.tv_usec/1000000.0);
#endif
}
static int torch_Timer_new(lua_State *L)
{
Timer *timer = luaT_alloc(L, sizeof(Timer));
#ifdef _MSC_VER
timer->base_time = 0;
while(!timer->base_time)
time(&timer->base_time);
#endif
timer->isRunning = 1;
timer->totalrealtime = 0;
timer->totalusertime = 0;
timer->totalsystime = 0;
timer->startrealtime = torch_Timer_realtime();
timer->startusertime = torch_Timer_usertime();
timer->startsystime = torch_Timer_systime();
luaT_pushudata(L, timer, "torch.Timer");
return 1;
}
static int torch_Timer_reset(lua_State *L)
{
Timer *timer = luaT_checkudata(L, 1, "torch.Timer");
timer->totalrealtime = 0;
timer->totalusertime = 0;
timer->totalsystime = 0;
timer->startrealtime = torch_Timer_realtime();
timer->startusertime = torch_Timer_usertime();
timer->startsystime = torch_Timer_systime();
lua_settop(L, 1);
return 1;
}
static int torch_Timer_free(lua_State *L)
{
Timer *timer = luaT_checkudata(L, 1, "torch.Timer");
luaT_free(L, timer);
return 0;
}
static int torch_Timer_stop(lua_State *L)
{
Timer *timer = luaT_checkudata(L, 1, "torch.Timer");
if(timer->isRunning)
{
double realtime = torch_Timer_realtime() - timer->startrealtime;
double usertime = torch_Timer_usertime() - timer->startusertime;
double systime = torch_Timer_systime() - timer->startsystime;
timer->totalrealtime += realtime;
timer->totalusertime += usertime;
timer->totalsystime += systime;
timer->isRunning = 0;
}
lua_settop(L, 1);
return 1;
}
static int torch_Timer_resume(lua_State *L)
{
Timer *timer = luaT_checkudata(L, 1, "torch.Timer");
if(!timer->isRunning)
{
timer->isRunning = 1;
timer->startrealtime = torch_Timer_realtime();
timer->startusertime = torch_Timer_usertime();
timer->startsystime = torch_Timer_systime();
}
lua_settop(L, 1);
return 1;
}
static int torch_Timer_time(lua_State *L)
{
Timer *timer = luaT_checkudata(L, 1, "torch.Timer");
double realtime = (timer->isRunning ? (timer->totalrealtime + torch_Timer_realtime() - timer->startrealtime) : timer->totalrealtime);
double usertime = (timer->isRunning ? (timer->totalusertime + torch_Timer_usertime() - timer->startusertime) : timer->totalusertime);
double systime = (timer->isRunning ? (timer->totalsystime + torch_Timer_systime() - timer->startsystime) : timer->totalsystime);
lua_createtable(L, 0, 3);
lua_pushnumber(L, realtime);
lua_setfield(L, -2, "real");
lua_pushnumber(L, usertime);
lua_setfield(L, -2, "user");
lua_pushnumber(L, systime);
lua_setfield(L, -2, "sys");
return 1;
}
static int torch_Timer___tostring__(lua_State *L)
{
Timer *timer = luaT_checkudata(L, 1, "torch.Timer");
lua_pushfstring(L, "torch.Timer [status: %s]", (timer->isRunning ? "running" : "stopped"));
return 1;
}
static const struct luaL_Reg torch_Timer__ [] = {
{"reset", torch_Timer_reset},
{"stop", torch_Timer_stop},
{"resume", torch_Timer_resume},
{"time", torch_Timer_time},
{"__tostring__", torch_Timer___tostring__},
{NULL, NULL}
};
void torch_Timer_init(lua_State *L)
{
luaT_newmetatable(L, "torch.Timer", NULL, torch_Timer_new, torch_Timer_free, NULL);
luaL_register(L, NULL, torch_Timer__);
lua_pop(L, 1);
}