Efficient date / time calculation for registration in unix / win32

I am in a situation where, after analyzing and analyzing our system, I came to the conclusion that the logging component of the system is one of many bottle necks that occupy about 17% of the total working time - a lot of things are recorded.

At the same time, about 5% of the time spent by the journal is associated with creating a date / time stamp in ascii in the following format: YYYYMMDD HHMMSS.fff - we roughly write about 700 thousand lines per second. (approximately 700K x (localtime and gettimeofday) calls per second)

I was curious that those who use SOers to create temporary markers are effective.

Cross-platform solutions are welcome.

Note1: we looked at Boost.datetime - it's great, but too slow for our needs, std :: chrono is an ideal solution, however we, unfortunately, must support pre C ++ 11 compilers.

Note2: We implemented a simple optimization that calculates only a part of the date (yyyymmdd) in 24 hours and, therefore, has only 1 gettimeofday call in a row - it didn’t help much.

+5
source share
3 answers

If you have the ability to use C ++ 11, you should check std :: chrono .

Otherwise, the optimization will depend on the required resolution. I would ask if you really need timestamps during registration, or can timestamps with sequence information be useful?

Example:

<timestamp1> <seq_num_0> ...
<timestamp1> <seq_num_1> ...
....
<timestamp1> <seq_num_n-1> ...
<timestamp2> <seq_num_0> ...

, :

, . , , , . , .

, .

EDIT: , . , timestamp . , , , . , , .

2: BOOST , ++ 11, :

  • - API OS.
  • , , .

, - , - , .

+3

, :

struct log_entry {
    struct timeval timestamp;
    unsigned int code;
    union {
        struct param1 p1;
        struct param2 p2;
    };
};

paramN , , , ( ).

, .

0

: downvoters . , , . !

, timestamp , N ( ) - . 4 :

struct current_time_stamp {
    char timestr_[4][16];
    unsigned index_;
    unsigned subsecond_;
    const char *get () const { return timestr_[index_%4]; }
    void update () {
        // ... update string in timestr_[(index_+1)%4] ...
        // ... if (index_ + 1)%4 is zero, recompute subsecond_
        ATOMIC_INCREMENT(index_);
        // ... also need a memory barrier for timestr_ update
    }
};

The second permission for each journal will be read from the high-performance counter. DeadMG offers QueryPerformanceTimerfor Windows, but for Linux (and POSIX) there is clock_gettime. If, however, exceeding these implementations still reaches you, you can request the time stamp counter on the processor directly using the built-in assembly (see rdtscfor x86). The sub-element value deviates from the value recorded in the structure to obtain the correct offset.

If you can get away with writing the timestamp in binary format, this can get away from the formatting problem.

-1
source

All Articles