Blame ext/posix/sys/times.c

Packit 437b5e
/*
Packit 437b5e
 * POSIX library for Lua 5.1, 5.2 & 5.3.
Packit 437b5e
 * (c) Gary V. Vaughan <gary@vaughan.pe>, 2013-2015
Packit 437b5e
 * (c) Reuben Thomas <rrt@sc3d.org> 2010-2013
Packit 437b5e
 * (c) Natanael Copa <natanael.copa@gmail.com> 2008-2010
Packit 437b5e
 * Clean up and bug fixes by Leo Razoumov <slonik.az@gmail.com> 2006-10-11
Packit 437b5e
 * Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> 07 Apr 2006 23:17:49
Packit 437b5e
 * Based on original by Claudio Terra for Lua 3.x.
Packit 437b5e
 * With contributions by Roberto Ierusalimschy.
Packit 437b5e
 * With documentation from Steve Donovan 2012
Packit 437b5e
 */
Packit 437b5e
/***
Packit 437b5e
 Process Times.
Packit 437b5e
Packit 437b5e
@module posix.sys.times
Packit 437b5e
*/
Packit 437b5e
Packit 437b5e
#include <config.h>
Packit 437b5e
Packit 437b5e
#include <sys/times.h>
Packit 437b5e
Packit 437b5e
#include "_helpers.c"
Packit 437b5e
Packit 437b5e
#define pushtimefield(k,x) pushintegerfield((k), ((lua_Integer)x)/clk_tck)
Packit 437b5e
Packit 437b5e
/***
Packit 437b5e
Process times record.
Packit 437b5e
All times are measured in cpu seconds.
Packit 437b5e
@table PosixTms
Packit 437b5e
@int tms_utime time spent executing user instructions
Packit 437b5e
@int tms_stime time spent in system execution
Packit 437b5e
@int tms_cutime sum of *tms_utime* for calling process and its children
Packit 437b5e
@int tms_cstime sum of *tms_stime* for calling process and its children
Packit 437b5e
*/
Packit 437b5e
static int
Packit 437b5e
pushtms(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	static long clk_tck = 0;
Packit 437b5e
Packit 437b5e
	struct tms t;
Packit 437b5e
	clock_t elapsed = times(&t);
Packit 437b5e
Packit 437b5e
	if (elapsed == (clock_t) -1)
Packit 437b5e
		return pusherror(L, "times");
Packit 437b5e
Packit 437b5e
	if (clk_tck == 0)
Packit 437b5e
		clk_tck = sysconf(_SC_CLK_TCK);
Packit 437b5e
Packit 437b5e
	lua_createtable(L, 0, 5);
Packit 437b5e
Packit 437b5e
	/* record elapsed time, and save accounting to struct */
Packit 437b5e
	pushtimefield("elapsed", elapsed);
Packit 437b5e
Packit 437b5e
	/* record struct entries */
Packit 437b5e
	pushtimefield("tms_utime", t.tms_utime);
Packit 437b5e
	pushtimefield("tms_stime", t.tms_stime);
Packit 437b5e
	pushtimefield("tms_cutime", t.tms_cutime);
Packit 437b5e
	pushtimefield("tms_cstime", t.tms_cstime);
Packit 437b5e
Packit 437b5e
	settypemetatable("PosixTms");
Packit 437b5e
	return 1;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
/***
Packit 437b5e
Get the current process times.
Packit 437b5e
@function times
Packit 437b5e
@treturn[1] PosixTms time accounting for this process, if successful
Packit 437b5e
@return[2] nil
Packit 437b5e
@treturn[2] string error message
Packit 437b5e
@treturn[2] int errnum
Packit 437b5e
@see times(2)
Packit 437b5e
*/
Packit 437b5e
static int
Packit 437b5e
Ptimes(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	checknargs(L, 0);
Packit 437b5e
	return pushtms(L);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
static const luaL_Reg posix_sys_times_fns[] =
Packit 437b5e
{
Packit 437b5e
	LPOSIX_FUNC( Ptimes		),
Packit 437b5e
	{NULL, NULL}
Packit 437b5e
};
Packit 437b5e
Packit 437b5e
Packit 437b5e
LUALIB_API int
Packit 437b5e
luaopen_posix_sys_times(lua_State *L)
Packit 437b5e
{
Packit 437b5e
	luaL_register(L, "posix.sys.times", posix_sys_times_fns);
Packit 437b5e
	lua_pushliteral(L, "posix.sys.times for " LUA_VERSION " / " PACKAGE_STRING);
Packit 437b5e
	lua_setfield(L, -2, "version");
Packit 437b5e
Packit 437b5e
	return 1;
Packit 437b5e
}