summaryrefslogtreecommitdiffstats
path: root/toolkit/components/processtools/ProcInfo_linux.cpp
blob: 51aa35c62e056e32bbf61bef1fdb8be90b123d7c (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/ProcInfo.h"
#include "mozilla/ProcInfo_linux.h"
#include "mozilla/Sprintf.h"
#include "mozilla/Logging.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/ipc/GeckoChildProcessHost.h"
#include "nsMemoryReporterManager.h"
#include "nsWhitespaceTokenizer.h"

#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <dirent.h>

#define NANOPERSEC 1000000000.

namespace mozilla {

int GetCycleTimeFrequencyMHz() { return 0; }

// StatReader can parse and tokenize a POSIX stat file.
// see http://man7.org/linux/man-pages/man5/proc.5.html
//
// Its usage is quite simple:
//
// StatReader reader(pid);
// ProcInfo info;
// rv = reader.ParseProc(info);
// if (NS_FAILED(rv)) {
//     // the reading of the file or its parsing failed.
// }
//
class StatReader {
 public:
  explicit StatReader(const base::ProcessId aPid)
      : mPid(aPid), mMaxIndex(15), mTicksPerSec(sysconf(_SC_CLK_TCK)) {}

  nsresult ParseProc(ProcInfo& aInfo) {
    nsAutoString fileContent;
    nsresult rv = ReadFile(fileContent);
    NS_ENSURE_SUCCESS(rv, rv);
    // We first extract the file or thread name
    int32_t startPos = fileContent.RFindChar('(');
    if (startPos == -1) {
      return NS_ERROR_FAILURE;
    }
    int32_t endPos = fileContent.RFindChar(')');
    if (endPos == -1) {
      return NS_ERROR_FAILURE;
    }
    int32_t len = endPos - (startPos + 1);
    mName.Assign(Substring(fileContent, startPos + 1, len));

    // now we can use the tokenizer for the rest of the file
    nsWhitespaceTokenizer tokenizer(Substring(fileContent, endPos + 2));
    int32_t index = 2;  // starting at third field
    while (tokenizer.hasMoreTokens() && index < mMaxIndex) {
      const nsAString& token = tokenizer.nextToken();
      rv = UseToken(index, token, aInfo);
      NS_ENSURE_SUCCESS(rv, rv);
      index++;
    }
    return NS_OK;
  }

 protected:
  // Called for each token found in the stat file.
  nsresult UseToken(int32_t aIndex, const nsAString& aToken, ProcInfo& aInfo) {
    // We're using a subset of what stat has to offer for now.
    nsresult rv = NS_OK;
    // see the proc documentation for fields index references.
    switch (aIndex) {
      case 13:
        // Amount of time that this process has been scheduled
        // in user mode, measured in clock ticks
        aInfo.cpuTime += GetCPUTime(aToken, &rv);
        NS_ENSURE_SUCCESS(rv, rv);
        break;
      case 14:
        // Amount of time that this process has been scheduled
        // in kernel mode, measured in clock ticks
        aInfo.cpuTime += GetCPUTime(aToken, &rv);
        NS_ENSURE_SUCCESS(rv, rv);
        break;
    }
    return rv;
  }

  // Converts a token into a int64_t
  uint64_t Get64Value(const nsAString& aToken, nsresult* aRv) {
    // We can't use aToken.ToInteger64() since it returns a signed 64.
    // and that can result into an overflow.
    nsresult rv = NS_OK;
    uint64_t out = 0;
    if (sscanf(NS_ConvertUTF16toUTF8(aToken).get(), "%" PRIu64, &out) == 0) {
      rv = NS_ERROR_FAILURE;
    }
    *aRv = rv;
    return out;
  }

  // Converts a token into CPU time in nanoseconds.
  uint64_t GetCPUTime(const nsAString& aToken, nsresult* aRv) {
    nsresult rv;
    uint64_t value = Get64Value(aToken, &rv);
    *aRv = rv;
    if (NS_FAILED(rv)) {
      return 0;
    }
    if (value) {
      value = (value * NANOPERSEC) / mTicksPerSec;
    }
    return value;
  }

  base::ProcessId mPid;
  int32_t mMaxIndex;
  nsCString mFilepath;
  nsString mName;

 private:
  // Reads the stat file and puts its content in a nsString.
  nsresult ReadFile(nsAutoString& aFileContent) {
    if (mFilepath.IsEmpty()) {
      if (mPid == 0) {
        mFilepath.AssignLiteral("/proc/self/stat");
      } else {
        mFilepath.AppendPrintf("/proc/%u/stat", unsigned(mPid));
      }
    }
    FILE* fstat = fopen(mFilepath.get(), "r");
    if (!fstat) {
      return NS_ERROR_FAILURE;
    }
    // /proc is a virtual file system and all files are
    // of size 0, so GetFileSize() and related functions will
    // return 0 - so the way to read the file is to fill a buffer
    // of an arbitrary big size and look for the end of line char.
    char buffer[2048];
    char* end;
    char* start = fgets(buffer, 2048, fstat);
    fclose(fstat);
    if (start == nullptr) {
      return NS_ERROR_FAILURE;
    }
    // let's find the end
    end = strchr(buffer, '\n');
    if (!end) {
      return NS_ERROR_FAILURE;
    }
    aFileContent.AssignASCII(buffer, size_t(end - start));
    return NS_OK;
  }

  int64_t mTicksPerSec;
};

// Threads have the same stat file. The only difference is its path
// and we're getting less info in the ThreadInfo structure.
class ThreadInfoReader final : public StatReader {
 public:
  ThreadInfoReader(const base::ProcessId aPid, const base::ProcessId aTid)
      : StatReader(aPid) {
    mFilepath.AppendPrintf("/proc/%u/task/%u/stat", unsigned(aPid),
                           unsigned(aTid));
  }

  nsresult ParseThread(ThreadInfo& aInfo) {
    ProcInfo info;
    nsresult rv = StatReader::ParseProc(info);
    NS_ENSURE_SUCCESS(rv, rv);

    // Copying over the data we got from StatReader::ParseProc()
    aInfo.cpuTime = info.cpuTime;
    aInfo.name.Assign(mName);
    return NS_OK;
  }
};

nsresult GetCpuTimeSinceProcessStartInMs(uint64_t* aResult) {
  timespec t;
  if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t) == 0) {
    uint64_t cpuTime =
        uint64_t(t.tv_sec) * 1'000'000'000u + uint64_t(t.tv_nsec);
    *aResult = cpuTime / PR_NSEC_PER_MSEC;
    return NS_OK;
  }

  StatReader reader(0);
  ProcInfo info;
  nsresult rv = reader.ParseProc(info);
  if (NS_FAILED(rv)) {
    return rv;
  }

  *aResult = info.cpuTime / PR_NSEC_PER_MSEC;
  return NS_OK;
}

nsresult GetGpuTimeSinceProcessStartInMs(uint64_t* aResult) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

ProcInfoPromise::ResolveOrRejectValue GetProcInfoSync(
    nsTArray<ProcInfoRequest>&& aRequests) {
  ProcInfoPromise::ResolveOrRejectValue result;

  HashMap<base::ProcessId, ProcInfo> gathered;
  if (!gathered.reserve(aRequests.Length())) {
    result.SetReject(NS_ERROR_OUT_OF_MEMORY);
    return result;
  }
  for (const auto& request : aRequests) {
    ProcInfo info;

    timespec t;
    clockid_t clockid = MAKE_PROCESS_CPUCLOCK(request.pid, CPUCLOCK_SCHED);
    if (clock_gettime(clockid, &t) == 0) {
      info.cpuTime = uint64_t(t.tv_sec) * 1'000'000'000u + uint64_t(t.tv_nsec);
    } else {
      // Fallback to parsing /proc/<pid>/stat
      StatReader reader(request.pid);
      nsresult rv = reader.ParseProc(info);
      if (NS_FAILED(rv)) {
        // Can't read data for this proc.
        // Probably either a sandboxing issue or a race condition, e.g.
        // the process has been just been killed. Regardless, skip process.
        continue;
      }
    }

    // The 'Memory' value displayed in the system monitor is resident -
    // shared. statm contains more fields, but we're only interested in
    // the first three.
    static const int MAX_FIELD = 3;
    size_t VmSize, resident, shared;
    info.memory = 0;
    FILE* f = fopen(nsPrintfCString("/proc/%u/statm", request.pid).get(), "r");
    if (f) {
      int nread = fscanf(f, "%zu %zu %zu", &VmSize, &resident, &shared);
      fclose(f);
      if (nread == MAX_FIELD) {
        info.memory = (resident - shared) * getpagesize();
      }
    }

    // Extra info
    info.pid = request.pid;
    info.childId = request.childId;
    info.type = request.processType;
    info.origin = request.origin;
    info.windows = std::move(request.windowInfo);
    info.utilityActors = std::move(request.utilityInfo);

    // Let's look at the threads
    nsCString taskPath;
    taskPath.AppendPrintf("/proc/%u/task", unsigned(request.pid));
    DIR* dirHandle = opendir(taskPath.get());
    if (!dirHandle) {
      // For some reason, we have no data on the threads for this process.
      // Most likely reason is that we have just lost a race condition and
      // the process is dead.
      // Let's stop here and ignore the entire process.
      continue;
    }
    auto cleanup = mozilla::MakeScopeExit([&] { closedir(dirHandle); });

    // If we can't read some thread info, we ignore that thread.
    dirent* entry;
    while ((entry = readdir(dirHandle)) != nullptr) {
      if (entry->d_name[0] == '.') {
        continue;
      }
      nsAutoCString entryName(entry->d_name);
      nsresult rv;
      int32_t tid = entryName.ToInteger(&rv);
      if (NS_FAILED(rv)) {
        continue;
      }

      ThreadInfo threadInfo;
      threadInfo.tid = tid;

      timespec ts;
      if (clock_gettime(MAKE_THREAD_CPUCLOCK(tid, CPUCLOCK_SCHED), &ts) == 0) {
        threadInfo.cpuTime =
            uint64_t(ts.tv_sec) * 1'000'000'000u + uint64_t(ts.tv_nsec);

        nsCString path;
        path.AppendPrintf("/proc/%u/task/%u/comm", unsigned(request.pid),
                          unsigned(tid));
        FILE* fstat = fopen(path.get(), "r");
        if (fstat) {
          // /proc is a virtual file system and all files are
          // of size 0, so GetFileSize() and related functions will
          // return 0 - so the way to read the file is to fill a buffer
          // of an arbitrary big size and look for the end of line char.
          // The size of the buffer needs to be as least 16, which is the
          // value of TASK_COMM_LEN in the Linux kernel.
          char buffer[32];
          char* start = fgets(buffer, sizeof(buffer), fstat);
          fclose(fstat);
          if (start) {
            // The thread name should always be smaller than our buffer,
            // so we should find a newline character.
            char* end = strchr(buffer, '\n');
            if (end) {
              threadInfo.name.AssignASCII(buffer, size_t(end - start));
              info.threads.AppendElement(threadInfo);
              continue;
            }
          }
        }
      }

      // Fallback to parsing /proc/<pid>/task/<tid>/stat
      // This is needed for child processes, as access to the per-thread
      // CPU clock is restricted to the process owning the thread.
      ThreadInfoReader reader(request.pid, tid);
      rv = reader.ParseThread(threadInfo);
      if (NS_FAILED(rv)) {
        continue;
      }
      info.threads.AppendElement(threadInfo);
    }

    if (!gathered.put(request.pid, std::move(info))) {
      result.SetReject(NS_ERROR_OUT_OF_MEMORY);
      return result;
    }
  }

  // ... and we're done!
  result.SetResolve(std::move(gathered));
  return result;
}

}  // namespace mozilla