02-20-05 11:19 PM
Hi,
This is the code I used to test the performance of the Linux
proc-filesystem. It shows that there is too much of overhead by parsing
the /proc/self/task/<tid>/stat - file and too much work is done in the
kernel.
Some results are:
mcr@preston:~/tmp/threads/clock> g++ diff.C -pthread -O3 -Wall -W
-Wno-long-long
mcr@preston:~/tmp/threads/clock> time ./a.out
CPU Time: 8.58
real 0m17.599s
user 0m1.306s
sys 0m16.251s
Can anyone speed this up?
Best regards
Christian Panten
extern "C" {
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/resource.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <asm/unistd.h>
#include <sys/syscall.h>
#include <linux/unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
}
#include <sstream>
#include <iostream>
static int fd;
static int pos;
_syscall0(pid_t,gettid)
void Timer_Init()
{
std::stringstream ss;
ss << "/proc/self/task/" << gettid() << "/stat";
fd = open(ss.str().c_str(), O_RDONLY);
if (fd < 0) {
std::cerr << "Could not open stat file\n";
exit(-1);
}
char buffer[1000];
pos = read(fd, buffer, 1000);
if (pos < 0) {
std::cerr << "Could not read stat file\n";
exit(-1);
}
while (buffer[--pos] != ')');
pos += 4;
}
double get_cpuusage()
{
lseek(fd, pos, SEEK_SET);
char buffer[1000];
read(fd, buffer, 1000);
int curpos = 0;
for (int i = 0; i != 10; ++i, ++curpos)
{
while (buffer[curpos] != ' ') ++curpos;
}
unsigned long long utime, stime;
sscanf(buffer+curpos, "%Lu %Lu", &utime, &stime);
return (double) (utime+stime) / 100.0;
}
double get_cpuusage_times()
{
static long long clockspersec = sysconf(_SC_CLK_TCK);
struct tms tm;
times(&tm);
return (double) (tm.tms_utime + tm.tms_stime) / clockspersec;
}
static double cputime = 0.0;
void func()
{
double cpuTime0 = get_cpuusage();
for (int i = 0; i != 100; ++i);
double cpuTime1 = get_cpuusage();
cputime += cpuTime1 - cpuTime0;
}
// void func()
// {
// double cpuTime0 = get_cpuusage_times();
// for (int i = 0; i != 100; ++i);
// double cpuTime1 = get_cpuusage_times();
// cputime += cpuTime1 - cpuTime0;
// }
extern "C" void * execute(void *)
{
Timer_Init();
for (int i = 0; i != 1000000; ++i)
func();
std::cout << "CPU Time: " << cputime << std::endl;
return 0;
}
int main()
{
pthread_t tid;
pthread_create(&tid, 0, execute, 0);
pthread_join(tid, 0);
}
[ Post a follow-up to this message ]
|