Efficient logging of time measurements


 
Thread Tools Search this Thread
Top Forums Programming Efficient logging of time measurements
# 1  
Old 02-06-2013
Linux Efficient logging of time measurements

Hi all,

I am working on a software, which was developed using C programming on unix environment. I have added certain functionality to the software.

Now, I interested in measuring time taken by new functionality (Mean value of huge amount of tests). Since new function will be part of many threads and will be running simultaneously.

I am looking for a efficient logging mechanism to log time duration for each execution, that will not reduce the performance.

I hope appending to a log file creates bottleneck.

FYI. This software is real time one and will be running for many weeks.

Thanks in advance,

Tamil
# 2  
Old 02-06-2013
First of all I'd like to state that I'm a not an expert in C.
I use in C++ the boost::timer library which is an excellent one to do those calculations.

If you don't want to add dependencies to your project you can do the following:
when the function that you want to calculate time starts start a thread that will count time : see man 3 usleep or man 3 sleep or man 3 nanosleep.
This thread sleeps a number of time which increase every n time unit.
This thread stops counting when the other function finish the procedure and change a boolean value to true.
I think the above solution will not be really efficient but it can work.

Experts in C should have a better answer to this question.
# 3  
Old 02-06-2013
Thanks Venam,

I feel creating a new thread for each thread in focus, would be a bad idea. I am thinking of using time() system call to measure time differences. There should not be any race conditions among the threads, in calling time().

However, once measurement is taken, there is a race condition when multiple threads trying to log. Using semaphore will in turn will create bottleneck.

I am not familiar with syslog(). Can that be useful here?

/Tamil
# 4  
Old 02-06-2013
I didn't get your point about the bottleneck.
In the world of threads there is something called mutex.
paulbridger.com/mutexes
You can also check timer_create() .

Maybe you can start only one thread that count the time and this thread checks for the status of all the other thread and saves in an array/struct the time they are done.
# 5  
Old 02-06-2013
Why do we need a thread to count seconds, when there are systems functions like time() ?

Mutex is also known as semaphore, using which will make threads blocking, which is a bottleneck.

/Tamil
# 6  
Old 02-07-2013
OK, for a real-time app, whatever you do has to be FAST, and if the resources aren't available for your processing thread to do the logging, I'm assuming you'll just have to skip logging that event.

Just as importantly, whatever logging you add, the performance has to be deterministic - it takes the same overhead to do the logging for a processing thread every single time you do the logging. That number should be as small as possible, but it can't be good most every time, with an occasional long-running hang waiting for resources.

So something like Boost is probably the last thing you should be looking into using. Do you know what's going on inside the Boose libraries? And how using it will impact your real-time processing in every single case? No, you don't. If you could get the entire Boost development team and have them answer, "Tell me exactly what happens when I use this Boost functionality in every possible use case.", even the Boost developers themselves almost certainly wouldn't be able to tell you.

Using syslog() might meet your needs, but I'd measure the overhead of doing so.

If I were to roll my own, I'd probably try something like this:

1. Define a binary structure:

Code:
struct timed_event
{
    int event_id;
    time_t event_start;
    time_t event_end;
    time_t current_time;
}

2. Define event ids for all the events you want to collect data from.

3. Create a pipe of some kind - named pipe, SYS V message queue, etc.

4. For each event you time, fill in one of those structures.

5. After filling in the structure, write the binary structure to the pipe. Write using a non-blocking low-level call such as a simple write(). Do NOT use C++ functionality, for a lot of reasons. Again, this needs to be non-blocking so if whatever pipe/queue you're writing to is full, you don't wait. It also needs to be atomic - which is why YOU need to make the write() call (or whatever) directly in YOUR code.

6. Write another process that reads the binary data from the pipe (should be easy since the reader knows the size of the binary structure - read() the proper number of bytes and get a filled-in struct), translates it into a readable log message, and logs it. Or maybe you just log the raw binary data and translate it offline later depending on your space needs.

You might want to add more data to the structure.

One thing to watch out for, especially if you implement something like a simple write() to a Unix pipe: partial write()'s. For example, if your structure comes out to be 32 bytes, maybe the pipe is almost full and write() only puts 24 bytes in the pipe. You might want do so something like start each structure with a magic number int field and put something in there that hopefully can't be duplicated in any other field. Then the reading/logging process can use that magic number field to try to sync up the binary stream of data.

Another issue: make sure your processing code and your logging code use the same size for the binary structure. Compiler optimization flags can change data alignments, which can change structure sizes. So if you aggressively optimize the processing code but not the logging code, the processing code could be using a binary structure that's 48 bytes because of alignment padding while the logging process could be using a non-padded structure that's 32 bytes.
# 7  
Old 02-07-2013
The idea of using a pipe is pretty cool. I never taught about that. Next time I do something I'll take that into consideration. It's going to be way faster But isn't it going to be a little harsh on I/O, I mean it depends on the interval the reader will loop in the pipe.
Anyway, you're answer is cool.

Last edited by venam; 02-07-2013 at 09:42 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Syslog not logging successful logging while unlocking server's console

When unlocking a Linux server's console there's no event indicating successful logging Is there a way I can fix this ? I have the following in my rsyslog.conf auth.info /var/log/secure authpriv.info /var/log/secure (1 Reply)
Discussion started by: walterthered
1 Replies

2. Shell Programming and Scripting

Help with Efficient Looping

Hello guys My requirement is to read a file with parent-child relationship we need to iterate through each row to find its latest child. for eg. parent child ABC PQR PQR DEF DEF XYZ Expected Output ABC XYZ PQR XYZ DEF XYZ Script Logic : read parent from file seach child... (4 Replies)
Discussion started by: joshiamit
4 Replies

3. UNIX for Advanced & Expert Users

Efficient way to grep

Hi Experts, I've been trying simple grep to search for a string in a huge number of files in a directory. grep <pattern> * this gives the search results as well as the following - grep: <filename>: Permission denied grep: <filename>: Permission denied for files which I don't have... (4 Replies)
Discussion started by: sumoka
4 Replies

4. Post Here to Contact Site Administrators and Moderators

Constant Logging In (After Logging Out)

Hi Everyone. First, I want to thank all of you for letting me participate in this great group. I am having a bit of a problem. After I get an email from a responder, I login to make my reply. In the mean time I get another response by email from another member, I go to reply to them and I... (6 Replies)
Discussion started by: Ccccc
6 Replies

5. UNIX for Advanced & Expert Users

efficient repace

some of the data i receive has been typed in manually due to which there are often places where i find 8 instead of ( and the incorrect use of case what according to you is the best way to correct such data. The data has around 20,000 records. The value i want to change is in the 4th field.... (2 Replies)
Discussion started by: VGR
2 Replies

6. Shell Programming and Scripting

Is there a way to make this more efficient

I have the following code. printf "Test Message Report" > report.txt while read line do msgid=$(printf "%n" "$line" | cut -c1-6000| sed -e 's///g' -e 's|.*ex:Msg\(.*\)ex:Msg.*|\1|') putdate=$(printf "%n" "$line" | cut -c1-6000| sed -e 's///g' -e 's|.*PutDate\(.*\)PutTime.*|\1|')... (9 Replies)
Discussion started by: gugs
9 Replies

7. Shell Programming and Scripting

Can you suggest a more efficient way for this?

Hi I have the following at the end of a service shutdown script used in part of an active-passive failover setup: ### # Shutdown all primary Network Interfaces # associated with failover ### # get interface names based on IP's # and shut them down to simulate loss of # heartbeatd ... (1 Reply)
Discussion started by: mikie
1 Replies

8. Shell Programming and Scripting

Efficient way of Awk

Hi, Can someone let me know if the below AWK can be made much simpler / efficient ? I have 200 fields, I need to substr only the last fields. So i'm printing awk -F~ 'print {$1, $2, $3....................................$196,$197 , susbstr($198,1,3999), substr($199,1,3999)..}' Is there a... (4 Replies)
Discussion started by: braindrain
4 Replies

9. Shell Programming and Scripting

Is there a more efficient way?

I'm using korn shell to connect to oracle, retrieve certain values, put them in a list, and iterate through them. While this method works, I can't help but think there is an easier method. If you know of one, please suggest a shorter, more efficient method. ############### FUNCTIONS ... (6 Replies)
Discussion started by: SelectSplat
6 Replies

10. UNIX for Advanced & Expert Users

Efficient Dispatching

Does anyone know what's new with Efficient dispatching in the Solaris 2.8 release (vs Solaris 2.6) release? Specifically, does anyone know of a good website to get detailed information on thread dispatching using efficient dispatching in solaris 2.8? Thank you. (1 Reply)
Discussion started by: uchachra
1 Replies
Login or Register to Ask a Question