Question: Write a simple C/C++ program to measure / estimate the overhead of system calls vs. procedure (function) calls. We will compare the overhead of the

Write a simple C/C++ program to measure / estimate the overhead of system calls vs. procedure (function) calls.

We will compare the overhead of the standard Unix/Linux system call getpid(), which simply returns the PID of the calling process with the call to a dummy function which performs a simple and trivial operation and returns. It seems appropriate to compare the dummy function to the getpid() system call since both dont perform any work other than returning a single value.

Use the Unix/Linux timing function clock_gettime() to measure the overhead of system calls and procedure calls.). If offers high resolution timing measurements. The basic idea is to measure the total time of many repeated system calls and the dummy function calls. The following code shows you how you can measure the execution time:

#include

#include

#include

#include

#define NO_ITERATIONS 1000000

#define NANOSECONDS_PER_SECOND 1000000000

typedef struct timespec timespec;

int dummyfunc(int x)

{

int y=10;

y=y+x;

return y;

}

/* return the number of nanoseconds between two timestamps */

unsigned long diff(timespec start, timespec end)

{

timespec temp;

if ((end.tv_nsec-start.tv_nsec)<0) {

temp.tv_sec = end.tv_sec-start.tv_sec-1;

temp.tv_nsec = NANOSECONDS_PER_SECOND+end.tv_nsec-start.tv_nsec;

} else {

temp.tv_sec = end.tv_sec-start.tv_sec;

temp.tv_nsec = end.tv_nsec-start.tv_nsec;

}

return (temp.tv_sec * NANOSECONDS_PER_SECOND + temp.tv_nsec);

}

int main()

{

timespec time1, time2;

double average_syscall, average_proccall;

int a;

clock_gettime(CLOCK_MONOTONIC, &time1);

for (int i = 0; i< NO_ITERATIONS; i++)

a = getpid();

clock_gettime(CLOCK_MONOTONIC, &time2);

average_syscall = (double)diff(time1,time2)/(double)NO_ITERATIONS;

printf("Average syscall time: %lf ", average_syscall);

clock_gettime(CLOCK_MONOTONIC, &time1);

for (int i = 0; i< NO_ITERATIONS; i++)

a = dummyfunc(20);

clock_gettime(CLOCK_MONOTONIC, &time2);

average_proccall = (double)diff(time1,time2)/(double)NO_ITERATIONS;

printf("Average procedure call time: %lf ", average_proccall);

return 0;

}

You can start with the above program and modify it to print the following information:

  1. [1 Point] Print the average system call execution time, i.e., the total time divided by 1,000,000 repeated executions of getpid() (the above program already does that).

  1. [1 Point] Print the average dummy function call execution time, i.e., the total time divided by 1,000,000 repeated executions of the dummy function (the above program already does that).

  1. [3 Points] Rather than measure the total time for 1000000 executions, measure every system call and function call individually, and store the individual measurements in two arrays (one for system calls and one for function calls). Repeat those measurements a reasonable number of times (example: 10,000 iterations).

  1. [3 Points] From the individual measurements, calculate the mean and standard deviations and print them. Run the program on a Linux servers. Write down the values reported by your program.

  1. [2 Points] Discuss your results.

  1. [2 Points] Intuitively, the mean values calculated from measuring many individual calls vs. a single measurement of many repeated calls should by similar. In practice they differ quite a bit. Try to explain why that happens.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!