mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-06-03 17:00:50 +00:00
cleanups, bug fixing
This commit is contained in:
parent
ade3d30c58
commit
4d9f3d9f7e
17
core/log.c
17
core/log.c
|
@ -333,24 +333,19 @@ int l_log( lua_State * L )
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LUA_TBOOLEAN :
|
case LUA_TBOOLEAN :
|
||||||
if( lua_toboolean( L, i ) )
|
|
||||||
lua_pushstring( L, "(true)" );
|
lua_pushstring( L, lua_toboolean( L, i ) ? "(true)" : "(false)" );
|
||||||
else
|
|
||||||
lua_pushstring( L, "(false)" );
|
|
||||||
|
|
||||||
lua_replace( L, i );
|
lua_replace( L, i );
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LUA_TUSERDATA:
|
case LUA_TUSERDATA:
|
||||||
{
|
|
||||||
clock_t *c = ( clock_t * ) luaL_checkudata( L, i, "Lsyncd.jiffies" );
|
|
||||||
|
|
||||||
double d = *c;
|
lua_pushfstring( L, "(Timestamp: %f)", check_jiffies_arg( L, i ) );
|
||||||
d /= clocks_per_sec;
|
|
||||||
lua_pushfstring( L, "(Timestamp: %f)", d );
|
lua_replace( L, i );
|
||||||
lua_replace( L, i );
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LUA_TNIL:
|
case LUA_TNIL:
|
||||||
|
|
16
core/main.c
16
core/main.c
|
@ -176,10 +176,7 @@ masterloop(
|
||||||
|
|
||||||
if( have_alarm )
|
if( have_alarm )
|
||||||
{
|
{
|
||||||
// TODO use trunc instead of long conversions
|
double d = time_diff( alarm_time, cnow, &tv );
|
||||||
double d = ( ( double )( alarm_time - cnow ) ) / clocks_per_sec;
|
|
||||||
tv.tv_sec = d;
|
|
||||||
tv.tv_nsec = ( ( d - ( long ) d ) ) * 1000000000.0;
|
|
||||||
|
|
||||||
printlogf(
|
printlogf(
|
||||||
L, "Masterloop",
|
L, "Masterloop",
|
||||||
|
@ -321,12 +318,6 @@ main1( int argc, char *argv[] )
|
||||||
// registers Lsycnd's core library
|
// registers Lsycnd's core library
|
||||||
register_core( L );
|
register_core( L );
|
||||||
|
|
||||||
if( check_logcat( "Debug" ) <= settings.log_level )
|
|
||||||
{
|
|
||||||
// printlogf doesnt support %ld :-(
|
|
||||||
printf( "kernels clocks_per_sec=%ld\n", clocks_per_sec );
|
|
||||||
}
|
|
||||||
|
|
||||||
signal_init( );
|
signal_init( );
|
||||||
|
|
||||||
#ifdef WITH_INOTIFY
|
#ifdef WITH_INOTIFY
|
||||||
|
@ -484,12 +475,11 @@ main1( int argc, char *argv[] )
|
||||||
int
|
int
|
||||||
main( int argc, char * argv[ ] )
|
main( int argc, char * argv[ ] )
|
||||||
{
|
{
|
||||||
// gets a kernel parameter
|
|
||||||
clocks_per_sec = sysconf( _SC_CLK_TCK );
|
|
||||||
|
|
||||||
setlinebuf( stdout );
|
setlinebuf( stdout );
|
||||||
setlinebuf( stderr );
|
setlinebuf( stderr );
|
||||||
|
|
||||||
|
time_first_init( );
|
||||||
|
|
||||||
while( true ) main1( argc, argv );
|
while( true ) main1( argc, argv );
|
||||||
|
|
||||||
// return -1;
|
// return -1;
|
||||||
|
|
|
@ -679,7 +679,7 @@ static const luaL_Reg corelib[ ] =
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
| Registers the Lsyncd's core library.
|
| Registers Lsyncd's core library.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
register_core( lua_State *L )
|
register_core( lua_State *L )
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
extern int l_stackdump( lua_State* L );
|
extern int l_stackdump( lua_State* L );
|
||||||
|
|
||||||
|
|
||||||
// FIXME
|
// Registers Lsyncd's core library.
|
||||||
extern void register_core( lua_State *L );
|
extern void register_core( lua_State *L );
|
||||||
|
|
||||||
|
|
||||||
|
|
78
core/time.c
78
core/time.c
|
@ -14,6 +14,7 @@
|
||||||
#include <sys/times.h>
|
#include <sys/times.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define LUA_USE_APICHECK 1
|
#define LUA_USE_APICHECK 1
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
@ -26,8 +27,7 @@
|
||||||
/*
|
/*
|
||||||
| The kernel's clock ticks per second.
|
| The kernel's clock ticks per second.
|
||||||
*/
|
*/
|
||||||
// FIXME make static again
|
static long clocks_per_sec;
|
||||||
long clocks_per_sec;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
| Non glibc builds need a real tms structure for the times( ) call
|
| Non glibc builds need a real tms structure for the times( ) call
|
||||||
|
@ -40,6 +40,17 @@ long clocks_per_sec;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
| Initializes time management.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
time_first_init( )
|
||||||
|
{
|
||||||
|
// gets a kernel parameter
|
||||||
|
clocks_per_sec = sysconf( _SC_CLK_TCK );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
| Returns the current time.
|
| Returns the current time.
|
||||||
*/
|
*/
|
||||||
|
@ -53,7 +64,7 @@ clock_t now( )
|
||||||
| Returns (on Lua stack) the current kernels clock state (jiffies).
|
| Returns (on Lua stack) the current kernels clock state (jiffies).
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
l_now(lua_State *L)
|
l_now( lua_State *L )
|
||||||
{
|
{
|
||||||
clock_t * j = lua_newuserdata( L, sizeof( clock_t ) );
|
clock_t * j = lua_newuserdata( L, sizeof( clock_t ) );
|
||||||
luaL_getmetatable( L, "Lsyncd.jiffies" );
|
luaL_getmetatable( L, "Lsyncd.jiffies" );
|
||||||
|
@ -79,16 +90,15 @@ l_jiffies_add( lua_State *L )
|
||||||
exit( -1 );
|
exit( -1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
clock_t a1 = p1 ? *p1 : luaL_checknumber( L, 1 ) * clocks_per_sec;
|
||||||
clock_t a1 = p1 ? *p1 : luaL_checknumber( L, 1 ) * clocks_per_sec;
|
clock_t a2 = p2 ? *p2 : luaL_checknumber( L, 2 ) * clocks_per_sec;
|
||||||
clock_t a2 = p2 ? *p2 : luaL_checknumber( L, 2 ) * clocks_per_sec;
|
clock_t *r = ( clock_t * ) lua_newuserdata( L, sizeof( clock_t ) );
|
||||||
clock_t *r = ( clock_t * ) lua_newuserdata( L, sizeof( clock_t ) );
|
|
||||||
|
|
||||||
luaL_getmetatable( L, "Lsyncd.jiffies" );
|
luaL_getmetatable( L, "Lsyncd.jiffies" );
|
||||||
lua_setmetatable( L, -2 );
|
lua_setmetatable( L, -2 );
|
||||||
*r = a1 + a2;
|
*r = a1 + a2;
|
||||||
return 1;
|
|
||||||
}
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -159,12 +169,13 @@ l_jiffies_lt( lua_State *L )
|
||||||
| True if jiffy1 is before or equals jiffy2.
|
| True if jiffy1 is before or equals jiffy2.
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
l_jiffies_le(lua_State *L)
|
l_jiffies_le( lua_State *L )
|
||||||
{
|
{
|
||||||
clock_t a1 = ( *( clock_t * ) luaL_checkudata( L, 1, "Lsyncd.jiffies" ) );
|
clock_t a1 = ( *( clock_t * ) luaL_checkudata( L, 1, "Lsyncd.jiffies" ) );
|
||||||
clock_t a2 = ( *( clock_t * ) luaL_checkudata( L, 2, "Lsyncd.jiffies" ) );
|
clock_t a2 = ( *( clock_t * ) luaL_checkudata( L, 2, "Lsyncd.jiffies" ) );
|
||||||
|
|
||||||
lua_pushboolean( L, ( a1 == a2 ) || time_before( a1, a2 ) );
|
lua_pushboolean( L, ( a1 == a2 ) || time_before( a1, a2 ) );
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,3 +208,44 @@ register_jiffies( lua_State *L )
|
||||||
lua_pop( L, 1 ); // pop( mt )
|
lua_pop( L, 1 ); // pop( mt )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
| Puts the time difference between 't2' (later) and 't1' into 'tv'.
|
||||||
|
| Returns the time difference as double value.
|
||||||
|
*/
|
||||||
|
double
|
||||||
|
time_diff(
|
||||||
|
long t2,
|
||||||
|
long t1,
|
||||||
|
struct timespec * tv
|
||||||
|
)
|
||||||
|
{
|
||||||
|
double d = ( ( double )( t2 - t1 ) ) / clocks_per_sec;
|
||||||
|
|
||||||
|
if( tv )
|
||||||
|
{
|
||||||
|
// TODO use trunc instead of long conversions
|
||||||
|
tv->tv_sec = d;
|
||||||
|
tv->tv_nsec = ( ( d - ( long ) d ) ) * 1000000000.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
| Checks if the function argument 'arg' on Lua stack is a jiffie
|
||||||
|
| and returns the value converted to seconds.
|
||||||
|
*/
|
||||||
|
double
|
||||||
|
check_jiffies_arg
|
||||||
|
(
|
||||||
|
lua_State *L,
|
||||||
|
int arg
|
||||||
|
)
|
||||||
|
{
|
||||||
|
clock_t *c = ( clock_t * ) luaL_checkudata( L, arg, "Lsyncd.jiffies" );
|
||||||
|
|
||||||
|
return ( ( double ) *c ) / clocks_per_sec;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
15
core/time.h
15
core/time.h
|
@ -16,20 +16,29 @@
|
||||||
#define time_before_eq(a,b) time_after_eq(b,a)
|
#define time_before_eq(a,b) time_after_eq(b,a)
|
||||||
|
|
||||||
|
|
||||||
// The kernel's clock ticks per second.
|
// Initializes time management.
|
||||||
extern long clocks_per_sec;
|
extern void time_first_init( );
|
||||||
|
|
||||||
|
|
||||||
// Returns the current time.
|
// Returns the current time.
|
||||||
extern clock_t now( );
|
extern clock_t now( );
|
||||||
|
|
||||||
|
|
||||||
|
// Puts the time difference between 't2' (later) and 't1' into 'tv'
|
||||||
|
extern double time_diff( long t2, long t1, struct timespec * tv );
|
||||||
|
|
||||||
|
|
||||||
// Returns (on Lua stack) the current kernels clock state( jiffies ).
|
// Returns (on Lua stack) the current kernels clock state( jiffies ).
|
||||||
extern int l_now(lua_State *L);
|
extern int l_now( lua_State *L );
|
||||||
|
|
||||||
|
|
||||||
// Registers the jiffies meta table in a Lua state.
|
// Registers the jiffies meta table in a Lua state.
|
||||||
extern void register_jiffies( lua_State *L );
|
extern void register_jiffies( lua_State *L );
|
||||||
|
|
||||||
|
|
||||||
|
// Checks if the function argument 'arg' on Lua stack is a jiffie
|
||||||
|
// and returns the value converted to seconds.
|
||||||
|
extern double check_jiffies_arg ( lua_State *L, int arg );
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -269,8 +269,7 @@ local eventFields =
|
||||||
end,
|
end,
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Returns true if the sync this event belongs
|
-- Returns true if the sync this event belongs to is stopped
|
||||||
-- to is stopped
|
|
||||||
--
|
--
|
||||||
syncStopped = function
|
syncStopped = function
|
||||||
(
|
(
|
||||||
|
@ -359,27 +358,36 @@ local eventMeta =
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Interface for user scripts to get list fields.
|
-- Interface for user script to get list fields.
|
||||||
|
--
|
||||||
|
local eventListFields =
|
||||||
|
{
|
||||||
|
--
|
||||||
|
-- Returns true if the sync this event list belongs to is stopped
|
||||||
|
--
|
||||||
|
syncStopped = function
|
||||||
|
(
|
||||||
|
dlist
|
||||||
|
)
|
||||||
|
return dlist.sync.stopped
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Interface for user scripts to get list functions.
|
||||||
--
|
--
|
||||||
local eventListFuncs =
|
local eventListFuncs =
|
||||||
{
|
{
|
||||||
--
|
--
|
||||||
-- Returns a list of paths of all events in list.
|
-- Returns a list of paths of all events in list.
|
||||||
--
|
--
|
||||||
--
|
|
||||||
getPaths = function
|
getPaths = function
|
||||||
(
|
(
|
||||||
elist, -- handle returned by getEvents( )
|
dlist, -- delayed list
|
||||||
mutator -- if not nil called with ( etype, path, path2 )
|
mutator -- if not nil called with ( etype, path, path2 )
|
||||||
-- returns one or two strings to add.
|
-- returns one or two strings to add.
|
||||||
)
|
)
|
||||||
local dlist = e2d[ elist ]
|
|
||||||
|
|
||||||
if not dlist
|
|
||||||
then
|
|
||||||
error( 'cannot find delay list from event list.' )
|
|
||||||
end
|
|
||||||
|
|
||||||
local result = { }
|
local result = { }
|
||||||
local resultn = 1
|
local resultn = 1
|
||||||
|
|
||||||
|
@ -407,8 +415,7 @@ local eventListFuncs =
|
||||||
end
|
end
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
end,
|
||||||
end
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -420,26 +427,22 @@ local eventListMeta =
|
||||||
__index = function
|
__index = function
|
||||||
(
|
(
|
||||||
elist,
|
elist,
|
||||||
func
|
field
|
||||||
)
|
)
|
||||||
if func == 'isList' then return true end
|
if field == 'isList' then return true end
|
||||||
|
|
||||||
if func == 'config' then return e2d[ elist ].sync.config end
|
-- FIXME make an actual field
|
||||||
|
if field == 'config' then return e2d[ elist ].sync.config end
|
||||||
|
|
||||||
local f = eventListFuncs[ func ]
|
local f = eventListFields[ field ]
|
||||||
|
|
||||||
if not f
|
if f then return f( e2d[ elist ] ) end
|
||||||
then
|
|
||||||
error(
|
|
||||||
'event list does not have function "' .. func .. '"',
|
|
||||||
2
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
return function
|
f = eventListFuncs[ field ]
|
||||||
( ... )
|
|
||||||
return f( elist, ... )
|
if not f then error( 'event list does not have field "' .. field .. '"', 2 ) end
|
||||||
end
|
|
||||||
|
return function( ... ) return f( e2d[ elist ], ... ) end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,19 @@ mt.__ipairs = function
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Returns sync at listpos key
|
||||||
|
--
|
||||||
|
mt.__index = function
|
||||||
|
(
|
||||||
|
self,
|
||||||
|
key
|
||||||
|
)
|
||||||
|
if type( key ) ~= 'number' then error( "key not a number" ) end
|
||||||
|
|
||||||
|
return syncList[ key ]
|
||||||
|
end
|
||||||
|
|
||||||
--
|
--
|
||||||
-- The round robin counter. In case of global limited maxProcesses
|
-- The round robin counter. In case of global limited maxProcesses
|
||||||
-- gives every sync equal chances to spawn the next process.
|
-- gives every sync equal chances to spawn the next process.
|
||||||
|
@ -72,13 +85,6 @@ local function getRound
|
||||||
return round
|
return round
|
||||||
end
|
end
|
||||||
|
|
||||||
--
|
|
||||||
-- Returns sync at listpos i
|
|
||||||
--
|
|
||||||
local function get
|
|
||||||
( i )
|
|
||||||
return syncList[ i ]
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local inherit
|
local inherit
|
||||||
|
@ -353,7 +359,6 @@ end
|
||||||
SyncMaster =
|
SyncMaster =
|
||||||
{
|
{
|
||||||
add = add, -- FIXME forward through metatable
|
add = add, -- FIXME forward through metatable
|
||||||
get = get, -- FIXME forward through metatable
|
|
||||||
remove = remove,
|
remove = remove,
|
||||||
getRound = getRound,
|
getRound = getRound,
|
||||||
concerns = concerns,
|
concerns = concerns,
|
||||||
|
|
|
@ -300,7 +300,7 @@ user.syncs =
|
||||||
|
|
||||||
for k = 0, #SyncMaster - 1
|
for k = 0, #SyncMaster - 1
|
||||||
do
|
do
|
||||||
list[ k ] = SyncMaster.get( k ):getUserIntf( )
|
list[ k ] = SyncMaster[ k ]:getUserIntf( )
|
||||||
end
|
end
|
||||||
|
|
||||||
return iter, list, -1
|
return iter, list, -1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user