How can I periodically call statistics / 2 in SWI-Prolog?

I am trying to test a program (SWI-) Prolog, which may take a few seconds. I would like to save CPU time and memory statistics in time, so that later I will show some evolutionary plot. Something similar to a system monitor, but only with my program.

For this, I tried to use alarm/4:

stat_start(Id) :-
    alarm(0.25,stat_point,Id,[remove(false),install(true)]). % 250 milliseconds

stat_stop(Id) :-
    remove_alarm(Id).

stat_point :-
    stat_cpu, % calls statistics/2 and appends values to a CSV file
    stat_mem. % calls statistics/2 and appends values to a CSV file

I just can't get the right time between each stat_point. The time varies from milliseconds to seconds, and there is nothing I can do about it. And the change of alarm/4time does not make any difference. I also tried a simple query like:

?- alarm(1, write('hello\n'), Id, [remove(false),install(true)]),
   repeat,
   fail.

. "". , , stat_point , , ? , .

Prolog ? profile/1 ​​ -?

+3
2

, , , . alarm/4 remove(false), uninstall_alarm/1 install_alarm/1, . remove_alarm/1, , , , - .

stat_mem/0 stat_cpu/0:

stat_start(Id,T) :-
    alarm(T,stat_point(Id,T),Id,[remove(false),install(true)]).

stat_point(Id,T) :-
    uninstall_alarm(Id),
    install_alarm(Id,T),
    stat_mem, 
    stat_cpu.

, /1 install_alarm/2. , 0, .

, "" , , stat_point ( ) , statistics/2 . , , magus - " ", , .

, , statistics/2, "" .

+2

, , , , , O.S. ( ?).

, , , ( ), , , O.S. . linux script sleep.

, , O.S. - , /os , /, , /, .

script / , " ", / . /, .

+2

All Articles