1
1
#include < fstream> /* std::ifstream */
2
2
#include < iostream> /* std::cout */
3
3
4
- // Timer includes
5
- // #include <boost/timer.hpp>
6
-
7
4
#include " include/GUTimer.h" /* Timers: time_h, rdtsc, chrono_hr, ctime, LOFAR_timer*/
8
5
#include < unistd.h>
9
-
6
+ #include < sys/stat.h>
7
+ #include < csignal> /* kill()*/
10
8
// =============
11
9
// =============
12
10
@@ -16,7 +14,6 @@ getio(const std::string& path)
16
14
17
15
std::ifstream file (path);
18
16
19
-
20
17
std::string readchars,dummy,wrchars,syscw,syscr,read_bytes,write_bytes;
21
18
file>>dummy>> readchars>>dummy>>wrchars>>dummy>>syscr>>dummy>>syscw>>dummy>>read_bytes>>dummy>>write_bytes;
22
19
@@ -49,22 +46,36 @@ getstat(const std::string& pid)//Get memory information (in one block right now)
49
46
}
50
47
51
48
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) {
62
51
struct stat buffer;
63
52
return (stat (name.c_str (), &buffer) == 0 );
64
53
}
65
54
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;
68
79
int delay=1000000 ;
69
80
if ( argc < 2 ){
70
81
std::cout<<" usage: " << argv[0 ] <<" PID configfile \n " ;
@@ -73,17 +84,18 @@ int main(int argc, char *argv[]) {
73
84
else
74
85
{ if (isdigit (argv[1 ][0 ])) // Process ID number
75
86
{
76
- pid_s = argv[1 ];
87
+ str_pid = argv[1 ];
77
88
}
78
89
else // Either *.cfg file or process launch (NOT implemented)
79
90
{
80
91
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
82
94
{
83
95
std::ifstream infile (configfile);
84
96
if (infile.good ())
85
97
{
86
- getline (infile,pnames );
98
+ getline (infile,str_pname );
87
99
getline (infile,delays);
88
100
}
89
101
if (delays==" " )
@@ -95,46 +107,59 @@ int main(int argc, char *argv[]) {
95
107
delay=1000 *std::stoi (delays);
96
108
}
97
109
infile.close ();
110
+ // Check if process name exists and get PID
111
+ getPiD (str_pid,str_pname);
98
112
}
99
- else
100
- {
101
- pnames=argv[1 ];
102
- }
103
- }
104
- }
105
113
114
+ else if (argv[1 ]!=" " ){ // launch process from here
115
+ std::cout<<" Launching process " <<configfile<<std::endl;
116
+ std::flush (std::cout);
106
117
118
+ int pid = fork ();
119
+ if (pid==0 )
120
+ {
121
+ int rc = execl (argv[1 ], " &" );
107
122
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
114
134
{
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);
119
138
}
120
139
}
121
140
}
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;
123
147
124
148
std::cout<<" $proc-m VmSize(pg), VMRSS (pg), shared-pages, code, 0, data+stack, 0\n " ;
125
149
std::cout<<" $proc-i rchar, wchar, syscr, syscw, read_bytes, write_bytes\n " ;
126
150
std::cout<<" $proc-s state, minflt, mjflt, utime ,s_time, nthreads, VMSize, RSS(pages)'\n ';" ;
127
151
// rusage ru;
128
152
time_t t = time (0 );
129
153
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;
131
155
132
156
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
134
159
{
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 ());
138
163
139
164
usleep (delay);
140
165
}
0 commit comments