473,657 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

kernel timer

Hey all,

I've been working on the code for a kernel timer which will report real
time, CPU time, user time and kernel time on the operation of fibonacci
method.

The way im doing it is keeping track of the parent and two child times
(using fork()).

I can get parent times to come up, however i do not think they are
correct, and i can not get child times to come up at all. I think in
the signal handler methods i have to somehow restart the timer or
signal, but i am not entirely sure how to.

All the code is below, can someone please assist me. Don't worry about
the delta_time method.

Thanks in advance for any help.
Michael M

#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

long unsigned int fibonacci(unsig ned int n);
long unsigned int elapsed_usecs(l ong, long);
long unsigned int delta_time(stru ct itimerval, struct itimerval);
void p_handler(int);
void c1_handler(int) ;
void c2_handler(int) ;

/* These variables are used to record the accumulated times. They
are set by the signal handlers and read by the processes when
they report the results */
static long p_realt_secs = 0, c1_realt_secs = 0, c2_realt_secs = 0;
static long p_virtt_secs = 0, c1_virtt_secs = 0, c2_virtt_secs = 0;
static long p_proft_secs = 0, c1_proft_secs = 0, c2_proft_secs = 0;
static struct itimerval p_realt, c1_realt, c2_realt;
static struct itimerval p_virtt, c1_virtt, c2_virtt;
static struct itimerval p_proft, c1_proft, c2_proft;

main(int argc, char **argv) {
long unsigned fib = 0;
int pid1, pid2;
unsigned int fibarg;
int status;

// Get command line argument, fibarg (N)
if(argc != 2) {
printf("usage: $./a.out <number>\n");
exit(EXIT_FAILU RE);
}
fibarg = (int)strtoul(ar gv[1], NULL, 10);
printf("Fib is: %u\n", fibarg);
// Initialize parent, child1 and child2 timer values
p_realt.it_inte rval.tv_sec = 1; //change
p_realt.it_inte rval.tv_usec = 0; // change
p_realt.it_valu e.tv_sec = 1; // change
p_realt.it_valu e.tv_usec = 0; // change

c1_realt.it_int erval.tv_sec = 0; //change
c1_realt.it_int erval.tv_usec = 0; // change
c1_realt.it_val ue.tv_sec = 0; // change
c1_realt.it_val ue.tv_usec = 0; // change

c2_realt.it_int erval.tv_sec = 0; //change
c2_realt.it_int erval.tv_usec = 0; // change
c2_realt.it_val ue.tv_sec = 0; // change
c2_realt.it_val ue.tv_usec = 0; // change

p_virtt.it_inte rval.tv_sec = 1; //change
p_virtt.it_inte rval.tv_usec = 0; // change
p_virtt.it_valu e.tv_sec = 1; // change
p_virtt.it_valu e.tv_usec = 0; // change

c1_virtt.it_int erval.tv_sec = 0; //change
c1_virtt.it_int erval.tv_usec = 0; // change
c1_virtt.it_val ue.tv_sec = 0; // change
c1_virtt.it_val ue.tv_usec = 0; // change

c2_virtt.it_int erval.tv_sec = 0; //change
c2_virtt.it_int erval.tv_usec = 0; // change
c2_virtt.it_val ue.tv_sec = 0; // change
c2_virtt.it_val ue.tv_usec = 0; // change

p_proft.it_inte rval.tv_sec = 1; //change
p_proft.it_inte rval.tv_usec = 0; // change
p_proft.it_valu e.tv_sec = 1; // change
p_proft.it_valu e.tv_usec = 0; // change

c1_proft.it_int erval.tv_sec = 0; //change
c1_proft.it_int erval.tv_usec = 0; // change
c1_proft.it_val ue.tv_sec = 0; // change
c1_proft.it_val ue.tv_usec = 0; // change

c2_proft.it_int erval.tv_sec = 0; //change
c2_proft.it_int erval.tv_usec = 0; // change
c2_proft.it_val ue.tv_sec = 0; // change
c2_proft.it_val ue.tv_usec = 0; // change

// Enable parents signal handlers
signal(SIGALRM, p_handler); // change to own signal handlers
signal(SIGVTALR M, p_handler); // change to own signal handlers
signal(SIGPROF, p_handler); // change to own signal handlers

// Set parents itimers
if(setitimer(IT IMER_REAL, &p_realt, NULL) == -1)
perror("parent real timer set error");
if(setitimer(IT IMER_VIRTUAL, &p_virtt, NULL) == -1)
perror("parent virtual timer set error");
if(setitimer(IT IMER_PROF, &p_proft, NULL) == -1)
perror("parent profile timer set error");

pid1 = fork();
if(pid1 == 0) {
// Enable child1 signal handlers (disable parent handlers)
signal(SIGALRM, c1_handler); // change to own signal handlers
signal(SIGVTALR M, c1_handler); // change to own signal
handlers
signal(SIGPROF, c1_handler);

// enable child1 signal handlers

// Set child1 itimers
if(setitimer(IT IMER_REAL, &c1_realt, NULL) == -1)
perror("child1 real timer set error");
if(setitimer(IT IMER_VIRTUAL, &c1_virtt, NULL) == -1)
perror("child1 virtual timer set error");
if(setitimer(IT IMER_PROF, &c1_proft, NULL) == -1)
perror("child1 profile timer set error");

// Start child1 itimer on the fibonacci program
fib = fibonacci(fibar g);

// Read child1 itimer values and report them
getitimer(ITIME R_PROF, &c1_proft);
getitimer(ITIME R_REAL, &c1_realt);
getitimer(ITIME R_VIRTUAL, &c1_virtt);
printf("\n");
printf("Child 1 fib = %ld, real time = %ld sec, %ld msec\n", fib,
c1_realt_secs, elapsed_usecs(c 1_realt.it_valu e.tv_sec,
c1_realt.it_val ue.tv_usec)/1000);

printf("Child 1 fib = %ld, cpu time = %ld sec, %ld msec\n", fib,
c1_proft_secs, elapsed_usecs(c 1_proft.it_valu e.tv_sec,
c1_proft.it_val ue.tv_usec)/1000);

printf("Child 1 fib = %ld, user time = %ld sec, %ld msec\n", fib,
c1_virtt_secs, elapsed_usecs(c 1_virtt.it_valu e.tv_sec,
c1_virtt.it_val ue.tv_usec)/1000);

printf("Child 1 fib = %ld, kernel time = %ld sec, %ld msec\n", fib,
delta_time(c1_p roft, c1_virtt),
(elapsed_usecs( c1_proft.it_val ue.tv_sec,
c1_proft.it_val ue.tv_usec)/1000) -
(elapsed_usecs( c1_virtt.it_val ue.tv_sec, c1_virtt.it_val ue.tv_usec)
/1000));

fflush(stdout);
exit(0);
} else {
pid2 = fork();
if(pid2 == 0) {
// Enable child2 signal handlers
signal(SIGALRM, c2_handler); // change to own signal handlers
signal(SIGVTALR M, c2_handler); // change to own signal
handlers
signal(SIGPROF, c2_handler);

// Set child2 itimers
if(setitimer(IT IMER_REAL, &c2_realt, NULL) == -1)
perror("child 2 real timer set error");
if(setitimer(IT IMER_VIRTUAL, &c2_virtt, NULL) == -1)
perror("child 2 virtual timer set error");
if(setitimer(IT IMER_PROF, &c2_proft, NULL) == -1)
perror("child 2 profile timer set error");

// Start child2 on the fibonacci program
fib = fibonacci(fibar g);

// Read child2 itimer vlues and report them
getitimer(ITIME R_PROF, &c2_proft);
getitimer(ITIME R_REAL, &c2_realt);
getitimer(ITIME R_VIRTUAL, &c2_virtt);
printf("\n");
printf("Child 2 fib = %ld, real time = %ld sec, %ld msec\n", fib,
c2_realt_secs, elapsed_usecs(c 2_realt.it_valu e.tv_sec,
c2_realt.it_val ue.tv_usec)/1000);

printf("Child 2 fib = %ld, cpu time = %ld sec, %ld msec\n", fib,
c2_proft_secs, elapsed_usecs(c 2_proft.it_valu e.tv_sec,
c2_proft.it_val ue.tv_usec)/1000);

printf("Child 2 fib = %ld, user time = %ld sec, %ld msec\n", fib,
c2_virtt_secs, elapsed_usecs(c 2_virtt.it_valu e.tv_sec,
c2_virtt.it_val ue.tv_usec)/1000);

printf("Child 2 fib = %ld, kernel time = %ld sec, %ld msec\n",
fib,
delta_time(c2_p roft, c2_virtt),
(elapsed_usecs( c2_proft.it_val ue.tv_sec,
c2_proft.it_val ue.tv_usec)/1000) -
(elapsed_usecs( c2_virtt.it_val ue.tv_sec,
c2_virtt.it_val ue.tv_usec)
/1000));

fflush(stdout);
exit(0);
} else { /* this is the parent */

// Start parent on the fibonacci program
fib = fibonacci(fibar g);

// Wait for children to terminate
waitpid(0, &status, 0);
waitpid(0, &status, 0);

// Read parent itimer values and report them
getitimer(ITIME R_PROF, &p_proft);
getitimer(ITIME R_REAL, &p_realt);
getitimer(ITIME R_VIRTUAL, &p_virtt);
printf("\n");
printf("Parent fib = %ld, real time = %ld sec, %ld msec\n", fib,
p_realt_secs, elapsed_usecs(p _realt.it_value .tv_sec,
p_realt.it_valu e.tv_usec)/1000);

printf("Parent fib = %ld, cpu time = %ld sec, %ld msec\n", fib,
p_proft_secs, elapsed_usecs(p _proft.it_value .tv_sec,
p_proft.it_valu e.tv_usec)/1000);

printf("Parent fib = %ld, user time = %ld sec, %ld msec\n", fib,
p_virtt_secs, elapsed_usecs(p _virtt.it_value .tv_sec,
p_virtt.it_valu e.tv_usec)/1000);

printf("Parent fib = %ld, kernel time = %ld sec, %ld msec\n", fib,
delta_time(p_pr oft, p_virtt),
(elapsed_usecs( p_proft.it_valu e.tv_sec,
p_proft.it_valu e.tv_usec)/1000) -
(elapsed_usecs( p_virtt.it_valu e.tv_sec, p_virtt.it_valu e.tv_usec)
/1000));

fflush(stdout);
exit(0);

}
printf("this line should never be printed\n");
}
}

long unsigned int fibonacci(unsig ned int n) {
if(n == 0)
return 0;
else if ( n == 1 || n == 2)
return 1;
else
return (fibonacci(n-1) + fibonacci(n-2));
}

long unsigned int elapsed_usecs(l ong sec, long usec)
{
return ((sec*1000000) + usec);
}

long unsigned int delta_time(stru ct itimerval n, struct itimerval m)
{
return m.it_value.tv_s ec + n.it_value.tv_s ec;
}

void p_handler(int signo)
{
switch(signo) {
case SIGALRM:
p_realt_secs++;
signal(SIGALRM, p_handler); // change to own signal handlers
break;
case SIGVTALRM:
p_virtt_secs++;
signal(SIGVTALR M, p_handler); // change to own signal handlers
break;
case SIGPROF:
p_proft_secs++;
signal(SIGPROF, p_handler);
break;
}
return;
}

void c1_handler(int signo)
{
switch(signo) {
case SIGALRM:
c1_realt_secs++ ;
signal(SIGALRM, c1_handler); // change to own signal handlers
break;
case SIGVTALRM:
c1_virtt_secs++ ;
signal(SIGVTALR M, c1_handler); // change to own signal handlers
break;
case SIGPROF:
c1_proft_secs++ ;
signal(SIGPROF, c1_handler);
break;
}
return;
}

void c2_handler(int signo)
{
switch(signo) {
case SIGALRM:
c2_realt_secs++ ;
break;
case SIGVTALRM:
c2_virtt_secs++ ;
break;
case SIGPROF:
c2_proft_secs++ ;
break;
}
return;
}

Nov 15 '05 #1
1 5661
ma********@hotm ail.com wrote:
I've been working on the code for a kernel timer which will report real
time, CPU time, user time and kernel time on the operation of fibonacci
method.

The way im doing it is keeping track of the parent and two child times
(using fork()).


[...]

I think you might be looking for comp.unix.progr ammer.

By the way, may I suggest splitting up your main() into more functions?

--
Sean
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3325
by: Steven T. Hatton | last post by:
http://netlab.ru.is/exception/LinuxCXX.shtml We have implemented a complete kernel level run-time support for C++ in the Linux kernel. In particular our run-time support enables the full use of C++ exceptions in the Linux kernel, but notably also includes support for global constructors and destructors, and dynamic type checking. Our kernel level support is based on open source commodity components, specifically the GNU gcc/g++ compiler...
2
2524
by: Rick | last post by:
Does anyone have any recommendations for setting kernel.shmmax and/or some of the other kernel parms that affect DB2, on a system that has 24-32GB of RAM available? I am also looking for a good resouce that describes what these parms really control. I want DB2 to take advantage of the large amount of RAM. These machines will have an average of 300-500 connections. The databases are about 300GB.
12
6353
by: Jean-Marc Blaise | last post by:
Hi, Is it worth to use 64-bit DB2 instances on a 32-bit kernel, in terms of: - performance - configuration (go beyond the 256 Mb segment for private mem, 1.75 Gb for Bufferpools) - other ? I've seen NIS is not supported, downlevel (64 to 32 bit ?) is not supported ....
1
1514
by: alice | last post by:
hi all, First of all I would apologize in case this the wrong group for posting this article. I'm trying to write small Linux kernel modules.I compile them with the following command gcc -c test.c which produces the file test.o
50
3488
by: Romeo Colacitti | last post by:
Is the C library of most OSes (i.e, unix type OSes) implemented at the very low kernel or just outside kernel level? Looking through the source tree of Linux/BSDs it seems like the C library is intertwined with the OS (string.h and other headers are there). So does this mean that when I program in C and use standard library functions, it's similar to calling the OS specific APIs (same type of performance)? Seeing all these Standard...
1
1878
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for queueing ip fragments as struct ipq { struct ipq *next; /* linked list pointers */ struct list_head lru_list; /* lru list member */ u32 user; u32 saddr;
1
2306
by: Marcel Groner | last post by:
I have a problem with postgresql runnung on smp kernel. setup: master: ------- - Pentium 4 (hyperthreading) - 2 GB Memory - os: fedora core 1 - kernel: 2.4.22-1.2188.nptlsmp
8
7590
by: db2_d_b_a | last post by:
Hello All, Brief info on the system: Db2 version : "DB2 v8.1.0.121" and FixPak "13". OS : RHEL 3 2.4.21-37.0.1 Memory : 8 GB Swap : 2 GB The issue here is the instance not being able to be started after the
20
6921
by: Jimmy | last post by:
Hi to all python now has grown to a versatile language that can accomplish tasks for many different purposes. However, AFAIK, little is known about its ability of kernel coding. So I am wondering if python can do some kernel coding that used to be the private garden of C/C++. For example, can python intercept the input of keyboard on a system level? someone told me it's a kernel thing, isn't it?
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.