issue #109 work on simple bytecode profiler
parent
4bb39d00f0
commit
36c99300a2
@ -0,0 +1,13 @@
|
||||
#ifndef RAVI_PROFILE_H
|
||||
#define RAVI_PROFILE_H
|
||||
|
||||
#include "lua.h"
|
||||
#include "lopcodes.h"
|
||||
|
||||
LUAI_DDEC unsigned long long raviV_profiledata[NUM_OPCODES];
|
||||
|
||||
LUAI_FUNC void raviV_init_profiledata(void);
|
||||
LUAI_FUNC void raviV_add_profiledata(OpCode opcode);
|
||||
LUAI_FUNC void raviV_print_profiledata(void);
|
||||
|
||||
#endif
|
@ -0,0 +1,62 @@
|
||||
-- Writen by Attractive Chaos; distributed under the MIT license
|
||||
|
||||
matrix = {}
|
||||
|
||||
function matrix.T(a: table)
|
||||
local m: integer, n: integer, x: table = #a, #a[1], {};
|
||||
for i = 1, n do
|
||||
local xi: number[] = table.numarray(n, 0.0)
|
||||
x[i] = xi
|
||||
for j = 1, m do xi[j] = @number (a[j][i]) end
|
||||
end
|
||||
return x;
|
||||
end
|
||||
|
||||
function matrix.mul(a: table, b: table)
|
||||
assert(#a[1] == #b);
|
||||
local m: integer, n: integer, p: integer, x: table = #a, #a[1], #b[1], {};
|
||||
local c: table = matrix.T(b); -- transpose for efficiency
|
||||
for i = 1, m do
|
||||
local xi: number[] = table.numarray(p, 0.0)
|
||||
x[i] = xi
|
||||
for j = 1, p do
|
||||
local sum: number, ai: number[], cj: number[] = 0.0, @number[](a[i]), @number[](c[j]);
|
||||
-- for luajit, caching c[j] or not makes no difference; lua is not so clever
|
||||
for k = 1, n do sum = sum + ai[k] * cj[k] end
|
||||
xi[j] = sum;
|
||||
end
|
||||
end
|
||||
return x;
|
||||
end
|
||||
|
||||
function matgen(n: integer)
|
||||
local a: table, tmp: number = {}, 1. / n / n;
|
||||
for i = 1, n do
|
||||
local ai: number[] = table.numarray(n, 0.0)
|
||||
a[i] = ai
|
||||
for j = 1, n do
|
||||
ai[j] = tmp * (i - j) * (i + j - 2)
|
||||
end
|
||||
end
|
||||
return a;
|
||||
end
|
||||
|
||||
--ravi.dumplua(matgen)
|
||||
|
||||
if ravi and ravi.jit() then
|
||||
ravi.compile(matrix.T)
|
||||
ravi.compile(matrix.mul)
|
||||
ravi.compile(matgen)
|
||||
end
|
||||
|
||||
local n = arg[1] or 1000;
|
||||
n = math.floor(n/2) * 2;
|
||||
if jit then
|
||||
-- luajit warmup
|
||||
matrix.mul(matgen(n), matgen(n))
|
||||
end
|
||||
local t1 = os.clock()
|
||||
local a = matrix.mul(matgen(n), matgen(n))
|
||||
local t2 = os.clock()
|
||||
print("time taken ", t2-t1)
|
||||
print(a[n/2+1][n//2+1]);
|
@ -0,0 +1,57 @@
|
||||
#include "ravi_profile.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
unsigned long long raviV_profiledata[NUM_OPCODES];
|
||||
|
||||
/* The number of nanoseconds in one second. */
|
||||
#define UV__NANOSEC 1000000000
|
||||
|
||||
/* Interval (in seconds) of the high-resolution clock. */
|
||||
static double hrtime_interval_ = 0;
|
||||
static uint64_t prev_time = 0;
|
||||
|
||||
void raviV_init_profiledata(void) {
|
||||
LARGE_INTEGER perf_frequency;
|
||||
/* Retrieve high-resolution timer frequency
|
||||
* and precompute its reciprocal.
|
||||
*/
|
||||
if (QueryPerformanceFrequency(&perf_frequency)) {
|
||||
hrtime_interval_ = 1.0 / perf_frequency.QuadPart;
|
||||
} else {
|
||||
hrtime_interval_= 0;
|
||||
}
|
||||
}
|
||||
|
||||
void raviV_add_profiledata(OpCode opcode) {
|
||||
LARGE_INTEGER counter;
|
||||
|
||||
/* If the performance interval is zero, there's no support. */
|
||||
if (hrtime_interval_ == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!QueryPerformanceCounter(&counter)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Because we have no guarantee about the order of magnitude of the
|
||||
* performance counter interval, integer math could cause this computation
|
||||
* to overflow. Therefore we resort to floating point math.
|
||||
*/
|
||||
uint64_t this_time = (uint64_t) ((double) counter.QuadPart * hrtime_interval_ * UV__NANOSEC);
|
||||
raviV_profiledata[opcode] += (this_time - (prev_time == 0 ? this_time : prev_time));
|
||||
prev_time = this_time;
|
||||
}
|
||||
void raviV_print_profiledata(void) {
|
||||
for (int i = 0; i < NUM_OPCODES; i++) {
|
||||
printf("PerfStat [%s] %llu\n", luaP_opnames[i], raviV_profiledata[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in new issue