Elapsed time for each rule

I have this bash code:

(At the beginning of the script :)

function timer() { if [[ $# -eq 0 ]]; then echo $(date '+%s') else local stime=$1 etime=$(date '+%s') if [[ -z "$stime" ]]; then stime=$etime; fi dt=$((etime - stime)) ds=$((dt % 60)) dm=$(((dt / 60) % 60)) dh=$((dt / 3600)) printf '%d:%02d:%02d' $dh $dm $ds fi } t=$(timer) 

(and, at the end of the script :)

 printf 'Elapsed time: %s\n' $(timer $t) 

to calculate the total elapsed time using a script. This code works fine in a bash (shell) script. So, I want to put this code in a makefile for each rule.

How can I put this function in a makefile? And how can you call 'in every rule?

I did something like this:

 define TIME stime=$(1) etime=$(date '+%s') dt=$((etime - stime)) \ ds=$((dt % 60)) \ ... endef 

and in each rule:

 rule1: dep1 dep2 dep3 ...SOME STUFF @$(call TIME, starttime) rule2: depx depD rule1 ...SOME STUFF @$(call TIME, starttime) 

but the math operation is not working. I have tried many things, but I can’t do the work.

+6
source share
3 answers

The problem is that in the bash script, the variable t stored from the very beginning (until work) to the end (when it can be subtracted from the end time). In the Make recipe, each line has its own wrapper, so the shell variable set in the earlier line will not be available in the later line.

You can combine all the recipe commands on one line so you can set t at the beginning and use it at the end, but it's pretty awkward. I suggest you write t to a file, possibly rule1_time , so that two timer calls do not require a common variable. Oh, and don't try to use call inside the command:

 STIME = date '+%s' > $@ _time ETIME = read st < $@ _time ; echo $$((`date '+%s'`-$$st)) all: $(STIME) do stuff $(ETIME) 

EDIT:
I wrote the code above as a proof of concept; I gathered for clarity, not grace. If I understand your comment correctly, you now want to know how to split the time into hours, minutes, and seconds without calling several functions from each rule. There are several ways to do this; this is perhaps the cleanest:

 ETIME = @read st < $@ _time ; st=$$((`date '+%s'`-$$st-68400)) ; echo Elapsed time: `date -d @$$st '+%H:%M:%S'` 
+5
source

If you are trying to get time information for the steps of your build, the best solution is to use smarter make. Electric Cloud's ElectricMake can generate an XML-labeled version of the build log, called an annotation file, which includes accurate sync data for each command caused by the build, as well as a boat of other information such as the exact command lines used (even if you used the @ prefix ) and environment variables used by each command.

You can download SparkBuild , the free version of ElectricMake, for free.

+1
source

you can use time:

  all:
     @@ time (\
         rsync -az $ {src1} $ {dest1} \
         && rsync -az $ {src2} $ {dest2} \
         ## etc.
     )

cannot use tabs here, but be sure to use only tabs in makefiles (as POSIX requires).

0
source

Source: https://habr.com/ru/post/887640/


All Articles