summaryrefslogtreecommitdiff
path: root/src/components/utils/src/resource_usage.cc
blob: 62c8d25b829347df29cd0922b65d0600ef2982ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "utils/resource_usage.h"
#if defined(__QNXNTO__)
#include <dirent.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/neutrino.h>
#include <sys/procfs.h>
#include <sys/stat.h>
#include <sys/trace.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#include <sys/resource.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sstream>
#include "utils/file_system.h"

namespace utils {

CREATE_LOGGERPTR_GLOBAL(logger_, "Utils")

const char* Resources::proc = "/proc/";

ResourseUsage* Resources::getCurrentResourseUsage() {
  PidStats pid_stats;
  if (false == GetProcInfo(pid_stats)) {
    LOG4CXX_ERROR(logger_, "Failed to get cpu proc info");
    return NULL;
  }
  MemInfo mem_info;
  if (false == GetMemInfo(mem_info)) {
    LOG4CXX_ERROR(logger_, "Failed to get memory info");
    return NULL;
  }
  ResourseUsage* usage = new ResourseUsage();
  usage->utime = pid_stats.utime;
  usage->stime = pid_stats.stime;
  usage->memory = static_cast<long long int>(mem_info);
  return usage;
}

bool Resources::ReadStatFile(std::string& output) {
  std::string filename = GetStatPath();
  if (false == file_system::FileExists(filename)) {
    return false;
  }
  if (false == file_system::ReadFile(filename,output)) {
    return false;
  }
  return true;
}

bool Resources::GetProcInfo(Resources::PidStats& output) {
#if defined(OS_LINUX)
  std::string proc_buf;
  if (false == ReadStatFile(proc_buf)) {
    return false;
  }
  uint32_t num_succes = sscanf(proc_buf.c_str(),
         "%d" //pid
         " %*s"//com
         " %c" //state
         " %d" //ppid
         " %d" //pgrp
         " %d" //session
         " %d" //tty_nr
         " %d" //tpgid
         " %u" //flags
         " %lu" //minflt
         " %lu" //cminflt
         " %lu" //majflt
         " %lu" //cmajflt
         " %lu" //utime
         " %lu" //stime
         " %ld" //cutime
         " %ld" //cstime
         " %ld" //priority
         " %ld" //nice
         " %ld" //num_threads
         " %ld" //itrealvalue
         " %llu" //starttime
         " %lu" //vsize
         " %ld" //rss
         " %lu" //rsslim
         " %lu" //startcode
         " %lu" //endcode
         " %lu" //startstack
         " %lu" //kstkesp
         " %lu" //kstkip
         " %lu" //signal
         " %lu" //blocked
         " %lu" //sigignore
         " %lu" //sigcatch
         " %lu" //wchan
         " %lu" //nswap
         " %lu" //cnswap
         " %d" //exit_signal
         " %d" //processor
         " %u" //rt_priority
         " %u" //policy
         " %llu" //delayacct_blkio_ticks
         " %lu" //guest_time
         " %ld" //cguest_time
         ,&(output.pid), &(output.state), &(output.ppid), &(output.pgrp), &(output.session),
         &(output.tty_nr), &(output.tpgid), &(output.flags), &(output.minflt), &(output.cminflt),
         &(output.majflt), &(output.cmajflt), &(output.utime), &(output.stime), &(output.cutime),
         &(output.cstime), &(output.priority), &( output.nice), &(output.num_threads), &(output.itrealvalue),
         &(output.starttime), &(output.vsize), &(output.rss), &(output.rsslim), &(output.startcode),
         &(output.endcode), &(output.startstack), &(output.kstkesp), &(output.kstkeip), &(output.signal),
         &(output.blocked), &(output.sigignore), &(output.sigcatch), &(output.wchan), &(output.nswap),
         &(output.cnswap), &(output.exit_signal), &(output.processor), &(output.rt_priority), &(output.policy),
         &(output.delayacct_blkio_ticks), &(output.guest_time), &(output.cguest_time)
  );
  if(num_succes != 43) { // 43 is number of iteams in Resources::PidStats
    LOG4CXX_ERROR(logger_, "Couldn't parse all iteams in /proc/PID/stat file");
    return false;
  }
  return true;
#elif defined(__QNXNTO__)
  int fd = open(GetProcPath().c_str(), O_RDONLY);
  if (0 >= fd) {
    LOG4CXX_ERROR(logger_, "Failed open process proc file : " << GetProcPath() <<
                  "; error no : " << strerror( errno ) );

    close(fd);
    return false;
  }
  devctl(fd, DCMD_PROC_INFO, &output, sizeof(output), 0);
  close(fd);
  return true;
#endif
}

bool Resources::GetMemInfo(Resources::MemInfo &output) {
  bool result = false;
  #if defined(OS_LINUX)
  Resources::PidStats pid_stat;
  if (false == GetProcInfo(pid_stat)) {
    LOG4CXX_ERROR(logger_, "Failed to get proc info");
    result = false;
  } else {
    output = pid_stat.vsize;
    result = true;
  }

#elif defined(__QNXNTO__)
  std::string as_path = GetStatPath();
  struct stat st;
  struct _dir* proc_dir = 0;
  if (0 == (proc_dir = opendir(proc))) {
    LOG4CXX_ERROR(logger_, "Unable to access to " << proc);
    result = false;
    return result;
  }  
  if (0 == readdir(proc_dir)) {
    LOG4CXX_ERROR(logger_, "Unable to read : " << proc_dir);
    closedir(proc_dir);
    result = false;
    return result;
  }
  closedir(proc_dir);
  if (-1 == stat(as_path.c_str(), &st) || 0 == st.st_size) {
     LOG4CXX_ERROR(logger_, "Unable to stat : " << as_path.c_str());
     result = false;
     return result;
  }
  output = st.st_size;
  result = true;
#endif
  return result;
}

std::string Resources::GetStatPath() {
  std::string filename;
#if defined(OS_LINUX)
  filename = GetProcPath() + "/stat";
#elif defined(__QNXNTO__)
  filename = GetProcPath() + "/as";
#endif
  return filename;
}

std::string Resources::GetProcPath() {
  char buffer[1024];
  pid_t my_pid = getpid();
  snprintf(buffer, sizeof(buffer), "%s%d/", proc , my_pid);
  std::string filename(buffer);
  return filename;
}

} // namespace utils