2011-06-28 9 views
3

/procディレクトリからこの情報を取得する方法はありますか?私は、各プロセスがどれくらいの時間(秒)で実行されているかを知ることができるようにしたい。プロセスの実行時間はどのくらいですか?

編集:これはC++から行う必要がありました。混乱させて申し訳ありません。

+0

あなたはどのくらいの_CPUのtime_を意味ですか、またはちょうど時計の時間? – Alnitak

答えて

4

だから、topコマンドのソースコードを読んだら、プロセスの開始時間を取得する非ハックな方法を見つけました。彼らが使用する式は次のとおりです。

Process_Time = (current_time - boot_time) - (process_start_time)/HZ. 

(あなたがprocess_start_timeはjiffy単位であるため、HZで分割する必要があり)

は、これらの値を取得する:

  • current_time - あなたはCからこれを取得することができますコマンドgettimeofday()
  • boot_time - この値は/proc/uptimeにあります。このファイルには、システムの稼働時間(秒)とアイドルプロセスで費やされた時間(秒)の2つの数値が含まれています。最初のものを取る。
  • process_start_time - この値は/proc/[PID]/statにあります。システム起動時とプロセス開始時の時間差(jiffies単位)。 (空白で分割した場合、ファイルの22番目の値)。

コード(申し訳ありませんが、私は時々、CおよびC++をミックス):

int fd; 
    char buff[128]; 
    char *p; 
    unsigned long uptime; 
    struct timeval tv; 
    static time_t boottime; 


    if ((fd = open("/proc/uptime", 0)) != -1) 
    { 
    if (read(fd, buff, sizeof(buff)) > 0) 
    { 
     uptime = strtoul(buff, &p, 10); 
     gettimeofday(&tv, 0); 
     boottime = tv.tv_sec - uptime; 

    } 
    close(fd); 
    } 


ifstream procFile; 
procFile.open("/proc/[INSERT PID HERE]/stat"); 

char str[255]; 
procFile.getline(str, 255); // delim defaults to '\n' 


vector<string> tmp; 
istringstream iss(str); 
copy(istream_iterator<string>(iss), 
    istream_iterator<string>(), 
    back_inserter<vector<string> >(tmp)); 

process_time = (now - boottime) - (atof(tmp.at(21).c_str()))/HZ; 

コーディングハッピー!

3

stat /proc/{processid}を実行すると、シェルでの作成時間を確認できます。

編集:fstatそのフォルダには、あなたが望むもの(作成時間)が表示されます。

+0

よろしくお願いします。フォルダが作成されたときをチェックするとは思わなかった。ありがとう! – kmdent

+0

私はそれがあなたにフォルダの作成時間を与えるとは思わない。それは、時間の変更、時間の変更、およびアクセス時間を与えます。どちらもあなたがフォルダが作成された時間を与えるものではありません。 – kmdent

+0

@kmdent:デフォルトでは、あなたがいないすべてのファイルシステムがそれをサポートするため、作成時間を取得しますが、表示されていないhttp://stackoverflow.com/questions/5929419/how-to-get-file-creation-date-in-linuxあなたに創造的な時間を与える潜在的な解決策のために。 – Femi

0

timeコマンドを使用すると、その情報を提供します:

> man 1 time 

コマンドライン引数は、それはあなたがあなたのPROGからコマンドを実行するsystem(char *command)を呼び出すことができます

%S  Total number of CPU-seconds that the process spent in kernel mode. 
%U  Total number of CPU-seconds that the process spent in user mode. 
%P  Percentage of the CPU that this job got 

を返すようになります。

1

のは、あなたが何をしようとして分解してみましょう:

  1. は、ファイルが変更された時刻を取得します。
  2. 時間をUnix時間に変換します。
  3. 2回を減算します。今

    #include <cstdio> 
    #include <cstdlib> 
    char *command; 
    int process_number = 1; // init process. 
    SYSTEM ("mkfifo time_pipe"); 
    sprintf (command, "stat /proc/%d -printf="%%X" > time_pipe", process_number); // get the command to run. 
    // since this directory is created once it starts, we know it is the start time (about) 
    // note the %%, which means to print a literal % 
    SYSTEM (command); // run the command. 
    

    を、次のステップは、Unixの時間にそれを解析している - しかし、我々はする必要はありません:

ので、現在の時間を得るために、我々は実行することができます! %X指定子は実際にそれをUnix Timeに変換します。次のステップは、(a)現在の時間を得ることです(b)時間を減算することです:

timeval cur_time; 
double current_time, time_passed; 
char read_time[11]; // 32 bit overflows = only 11 digits. 
FILE *ourpipe; 
gettimeofday(&cur_time, NULL); 
current_time = cur_time.tv_sec + (cur_time.tv_usec * 1000000.0); 
// usec stands for mu second, i.e., a millionth of a second. I wasn't there when they named this stuff. 
ourpipe = fopen ("time_pipe", "rb"); 
fread(read_time, sizeof (char), 10, ourpipe); 
time_passed = current_time - atoi (read_time); 
fclose (ourpipe); 

そうです、それはかなりです。パイプは、一方から他方への入力を得るために必要です。

+0

変更時刻は作成時刻とは異なります。 Statは修正時間、アクセス時間、および変更時間を与えます。これは実際にあなたにプロセス作成時間を与えません。同意しますか? – kmdent

+0

@kmdentはい、あなたは正しいと思われます。代わりに、 'ps -eo pid、etime'(pidの経過時間を返す)と仮定します。私はそれに応じて自分の投稿を更新します。 – Arka

0

は/ proc/{processidを}#グッドアイデア!

ただ、/ proc/{processid}/statを読んで、必要な統計情報を取得するだけではどうですか? 「男のPROC」から

 
... 
     stat kernel/system statistics

  cpu 3357 0 4313 1362393 
       The number of jiffies (1/100ths of a second) 
       that the system spent in user mode, user 
       mode with low priority (nice), system mode, 
       and the idle task, respectively. The last 
       value should be 100 times the second entry 
       in the uptime pseudo-file. 

      disk 0 0 0 0 
       The four disk entries are not implemented at 
       this time. I'm not even sure what this 
       should be, since kernel statistics on other 
       machines usually track both transfer rate 
       and I/Os per second and this only allows for 
       one field per drive. 

...

0

旧トピックこの、私は同じ問題に取り組んでいたことから、私は私が私の応答を投稿するかもしれないと思いました。おそらくそれは他の人にとって有益だろう。このコードは深刻な生産環境で使用するべきではありませんが、OPが探しているものを得るためのすばやく汚れた方法として、これで十分だろうと思います。このコードは、自分自身の質問に応じて投稿されたOPと同じコードですが、スタックエクスチェンジからコピーすると直接コンパイルできるように変更されています。コードは直接コンパイルできませんでした。

このコードはコンパイルして、私はいくつかの余分な機能を追加しました。

指示:|任意のプログラムを起動し、」PSの補助を行いますprogramid 'を使用してpidを取得します。それは左から2列目です。今度は、その番号をメイン関数のpidに入力し、プログラムをコンパイルします。

失効した:日:0、時間:0、分:5秒:プログラムを実行するときに今、出力は次のようになります58

//Original code credit by kmdent. 
//http://stackoverflow.com/questions/6514378/how-do-you-get-how-long-a-process-has-been-running 
#include <iostream> 
#include <iterator> 
#include <sstream> 
#include <fstream> 
#include <vector> 
#include <cstring> 
#include <cerrno> 
#include <ctime> 
#include <cstdio> 
#include <fcntl.h> 
#include <sys/time.h> 

#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <string> 
#include "/usr/include/x86_64-linux-gnu/sys/param.h" 

using namespace std; 


template <class T> 
inline std::string to_string (const T& t) 
{ 
    std::stringstream ss; 
    ss << t; 
    return ss.str(); 
} 

//Return the number of seconds a process has been running. 
long lapsed(string pid) { 

    int fd; 
    char buff[128]; 
    char *p; 
    unsigned long uptime; 
    struct timeval tv; 
    static time_t boottime; 


    if ((fd = open("/proc/uptime", 0)) != -1) { 
    if (read(fd, buff, sizeof(buff)) > 0) { 
     uptime = strtoul(buff, &p, 10); 
     gettimeofday(&tv, 0); 
     boottime = tv.tv_sec - uptime; 
    } 
     close(fd); 
    } 

    ifstream procFile; 
    string f = "/proc/"+pid+"/stat"; 
    procFile.open(f.c_str()); 

    char str[255]; 
    procFile.getline(str, 255); // delim defaults to '\n' 

    vector<string> tmp; 
    istringstream iss(str); 
    copy(istream_iterator<string>(iss), 
     istream_iterator<string>(), 
     back_inserter<vector<string> >(tmp)); 

    std::time_t now = std::time(0); 
    std::time_t lapsed = ((now - boottime) - (atof(tmp.at(21).c_str()))/HZ); 
    return lapsed; 

} 

string human_readable_lapsed(long input_seconds) { 
    //Credit: http://www.cplusplus.com/forum/beginner/14357/ 
    long days = input_seconds/60/60/24; 
    int hours = (input_seconds/60/60) % 24; 
    int minutes = (input_seconds/60) % 60; 
    int seconds = input_seconds % 60; 

    return "days: " + to_string(days) + " , hours: " + to_string(hours) + " , min: " + to_string(minutes) + " , seconds: " + to_string(seconds); 
} 

int main(int argc, char* argv[]) 
{ 
    //Pid to get total running time for. 
    string pid = "13875"; 
    std::cout << "Lapsed: " << human_readable_lapsed(lapsed(pid)) << std::endl; 
    return 0; 
} 
関連する問題