Skip to content

Commit c16a8b5

Browse files
committed
Added process fork, command-line printout
1 parent e9af3b3 commit c16a8b5

File tree

3 files changed

+67
-62
lines changed

3 files changed

+67
-62
lines changed

bin/Debug/procfs-sampler

423 Bytes
Binary file not shown.

include/customtimer.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

samplefile.cpp

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include <fstream> /* std::ifstream */
22
#include <iostream> /* std::cout */
33

4-
//Timer includes
5-
//#include <boost/timer.hpp>
6-
74
#include "include/GUTimer.h" /*Timers: time_h, rdtsc, chrono_hr, ctime, LOFAR_timer*/
85
#include <unistd.h>
9-
6+
#include <sys/stat.h>
7+
#include<csignal> /*kill()*/
108
//=============
119
//=============
1210

@@ -16,7 +14,6 @@ getio(const std::string& path)
1614

1715
std::ifstream file(path);
1816

19-
2017
std::string readchars,dummy,wrchars,syscw,syscr,read_bytes,write_bytes;
2118
file>>dummy>> readchars>>dummy>>wrchars>>dummy>>syscr>>dummy>>syscw>>dummy>>read_bytes>>dummy>>write_bytes;
2219

@@ -49,22 +46,36 @@ getstat(const std::string& pid)//Get memory information (in one block right now)
4946
}
5047

5148

52-
//template <typename Word>
53-
//std::ifstream& read_word( std::ifstream& ins, Word& value )
54-
// {
55-
// for (unsigned size = 0, value = 0; size < sizeof( Word ); ++size)
56-
// value |= ins.get() << (8 * size);
57-
// return ins;
58-
// }
59-
#include <sys/stat.h>
60-
61-
inline bool exists (const std::string& name) {
49+
inline bool
50+
exists (const std::string& name) {
6251
struct stat buffer;
6352
return (stat (name.c_str(), &buffer) == 0);
6453
}
6554

66-
int main(int argc, char *argv[]) {
67-
std::string pid_s,configfile="",pnames="",delays;
55+
void
56+
getPiD(std::string& str_pid, std::string& str_pname)
57+
{
58+
59+
if(str_pid=="")
60+
{
61+
while(str_pid=="")
62+
{
63+
FILE* fpidof = popen(("pidof "+str_pname).c_str(),"r");
64+
if (fpidof)
65+
{
66+
int p=0;
67+
if (fscanf(fpidof, "%d", &p)>0 && p>0)
68+
str_pid = std::to_string(p);
69+
pclose(fpidof);
70+
}
71+
}
72+
}
73+
return;
74+
}
75+
76+
int
77+
main(int argc, char *argv[]) {
78+
std::string str_pid,configfile="",str_pname="",delays,str_command;
6879
int delay=1000000;
6980
if ( argc < 2 ){
7081
std::cout<<"usage: "<< argv[0] <<" PID configfile \n";
@@ -73,17 +84,18 @@ int main(int argc, char *argv[]) {
7384
else
7485
{ if(isdigit(argv[1][0])) //Process ID number
7586
{
76-
pid_s= argv[1];
87+
str_pid= argv[1];
7788
}
7889
else //Either *.cfg file or process launch (NOT implemented)
7990
{
8091
configfile=argv[1];
81-
if (configfile!="" and configfile.find(".cfg"))
92+
93+
if (configfile.find(".cfg")!=std::string::npos) //if a configure file is specified
8294
{
8395
std::ifstream infile(configfile);
8496
if (infile.good())
8597
{
86-
getline(infile,pnames);
98+
getline(infile,str_pname);
8799
getline(infile,delays);
88100
}
89101
if (delays=="")
@@ -95,46 +107,59 @@ int main(int argc, char *argv[]) {
95107
delay=1000*std::stoi(delays);
96108
}
97109
infile.close();
110+
// Check if process name exists and get PID
111+
getPiD(str_pid,str_pname);
98112
}
99-
else
100-
{
101-
pnames=argv[1];
102-
}
103-
}
104-
}
105113

114+
else if(argv[1]!=""){ //launch process from here
115+
std::cout<<"Launching process "<<configfile<<std::endl;
116+
std::flush(std::cout);
106117

118+
int pid = fork();
119+
if(pid==0)
120+
{
121+
int rc = execl(argv[1], "&");
107122

108-
if(pid_s=="")
109-
{
110-
while(pid_s=="")
111-
{
112-
FILE* fpidof = popen(("pidof "+pnames).c_str(),"r");
113-
if (fpidof)
123+
if (rc==-1) std::cout<<"Couldn't launch process "<<configfile<<" Did you have the path right?"<<std::endl;
124+
}
125+
else //parent process
126+
{
127+
std::cout<<"Parent Process";
128+
std::string str_name="firefox";//extract name from path
129+
// Check if process name exists and get PID
130+
getPiD(str_pid,str_name);
131+
}
132+
}
133+
else //otherwise the process name to track
114134
{
115-
int p=0;
116-
if (fscanf(fpidof, "%d", &p)>0 && p>0)
117-
pid_s = std::to_string(p);
118-
pclose(fpidof);
135+
str_pname=argv[1];
136+
std::cout<<str_pname;
137+
std::flush(std::cout);
119138
}
120139
}
121140
}
122-
std::cout<<pid_s<<" "<<pnames<< " from "<<configfile<<" with delay (ms) "<<delay/1000<<std::endl;
141+
142+
std::cout<<"$proc "<<str_pid<<" "<<str_pname<< " from "<<configfile<<" with delay (ms) "<<delay/1000<<std::endl;
143+
144+
std::ifstream filec("/proc/"+str_pid+"/cmdline");
145+
filec>>str_command;
146+
std::cout<<"$proc cmd: "<<str_command<<std::endl;
123147

124148
std::cout<<"$proc-m VmSize(pg), VMRSS (pg), shared-pages, code, 0, data+stack, 0\n";
125149
std::cout<<"$proc-i rchar, wchar, syscr, syscw, read_bytes, write_bytes\n";
126150
std::cout<<"$proc-s state, minflt, mjflt, utime ,s_time, nthreads, VMSize, RSS(pages)'\n';";
127151
// rusage ru;
128152
time_t t = time(0);
129153
struct tm * now = localtime( & t );
130-
std::cout <<"$Start time "<< (now->tm_hour)<<":"<<(now->tm_min)<<":"<<(now->tm_sec) << std::endl;
154+
std::cout <<"$proc-Start time: "<< (now->tm_hour)<<":"<<(now->tm_min)<<":"<<(now->tm_sec) << std::endl;
131155

132156

133-
while(exists("/proc/"+pid_s+"/exe"))
157+
//while(exists("/proc/"+str_pid+"/exe"))//main loop, executes while the process is running (maybe faster way?)
158+
while(not(kill(std::stoi(str_pid),NULL))) //continues if pid exists, not sure if works fasters
134159
{
135-
printf(("$proc-io "+getio("/proc/"+pid_s+"/io")).c_str());
136-
printf(("$proc-mem " + getmem(pid_s)).c_str());
137-
printf(("$proc-stat "+getstat(pid_s)).c_str());
160+
printf(("$proc-io "+getio("/proc/"+str_pid+"/io")).c_str());
161+
printf(("$proc-mem " + getmem(str_pid)).c_str());
162+
printf(("$proc-stat "+getstat(str_pid)).c_str());
138163

139164
usleep(delay);
140165
}

0 commit comments

Comments
 (0)