summaryrefslogtreecommitdiffstats
path: root/toolkit/components/processtools/ProcInfo_linux.cpp
blob: 44831474457e98b5d22ae29421642785a8cb96e4 (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
/* -*- 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/Sprintf.h"
#include "mozilla/Logging.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/ipc/GeckoChildProcessHost.h"
#include "nsLocalFile.h"
#include "nsMemoryReporterManager.h"
#include "nsNetCID.h"
#include "nsWhitespaceTokenizer.h"

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

#define NANOPERSEC 1000000000.

namespace mozilla {

// 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(53), mTicksPerSec(sysconf(_SC_CLK_TCK)) {
    mFilepath.AppendPrintf("/proc/%u/stat", mPid);
  }

  nsresult ParseProc(ProcInfo& aInfo) {
    nsAutoString fileContent;
    nsresult rv = ReadFile(fileContent);
    NS_ENSURE_SUCCESS(rv, rv);
    // We first extract the filename
    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);
    aInfo.filename.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.cpuUser = 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.cpuKernel = GetCPUTime(aToken, &rv);
        NS_ENSURE_SUCCESS(rv, rv);
        break;
      case 23:
        // Resident Set Size: number of pages the process has
        // in real memory.
        uint64_t pageCount = Get64Value(aToken, &rv);
        NS_ENSURE_SUCCESS(rv, rv);
        uint64_t pageSize = sysconf(_SC_PAGESIZE);
        aInfo.residentSetSize = pageCount * pageSize;
        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;
  ProcInfo mProcInfo;

 private:
  // Reads the stat file and puts its content in a nsString.
  nsresult ReadFile(nsAutoString& aFileContent) {
    RefPtr<nsLocalFile> file = new nsLocalFile(mFilepath);
    bool exists;
    nsresult rv = file->Exists(&exists);
    NS_ENSURE_SUCCESS(rv, rv);
    if (!exists) {
      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.
    FILE* fstat;
    if (NS_FAILED(file->OpenANSIFileDesc("r", &fstat)) || !fstat) {
      return NS_ERROR_FAILURE;
    }
    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), mTid(aTid) {
    // Adding the thread path
    mFilepath.Truncate();
    mFilepath.AppendPrintf("/proc/%u/task/%u/stat", aPid, mTid);
    mMaxIndex = 17;
  }

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

    aInfo.tid = mTid;
    // Copying over the data we got from StatReader::ParseProc()
    aInfo.cpuKernel = info.cpuKernel;
    aInfo.cpuUser = info.cpuUser;
    aInfo.name.Assign(info.filename);
    return NS_OK;
  }

 private:
  base::ProcessId mTid;
};

RefPtr<ProcInfoPromise> GetProcInfo(nsTArray<ProcInfoRequest>&& aRequests) {
  auto holder = MakeUnique<MozPromiseHolder<ProcInfoPromise>>();
  RefPtr<ProcInfoPromise> promise = holder->Ensure(__func__);
  nsresult rv = NS_OK;
  nsCOMPtr<nsIEventTarget> target =
      do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
  if (NS_FAILED(rv)) {
    NS_WARNING("Failed to get stream transport service");
    holder->Reject(rv, __func__);
    return promise;
  }

  RefPtr<nsIRunnable> r = NS_NewRunnableFunction(
      __func__,
      [holder = std::move(holder), requests = std::move(aRequests)]() {
        HashMap<base::ProcessId, ProcInfo> gathered;
        if (!gathered.reserve(requests.Length())) {
          holder->Reject(NS_ERROR_OUT_OF_MEMORY, __func__);
          return;
        }
        for (const auto& request : requests) {
          // opening the stat file and reading its content
          StatReader reader(request.pid);
          ProcInfo info;
          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;
          }
          // Computing the resident unique size is somewhat tricky,
          // so we use about:memory's implementation. This implementation
          // reopens `/proc/[pid]`, so there is the risk of an additional
          // race condition. In that case, the result is `0`.
          info.residentUniqueSize =
              nsMemoryReporterManager::ResidentUnique(request.pid);

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

          // Let's look at the threads
          nsCString taskPath;
          taskPath.AppendPrintf("/proc/%u/task", 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;
            }
            // Threads have a stat file, like processes.
            nsAutoCString entryName(entry->d_name);
            int32_t tid = entryName.ToInteger(&rv);
            if (NS_FAILED(rv)) {
              continue;
            }
            ThreadInfoReader reader(request.pid, tid);
            ThreadInfo threadInfo;
            rv = reader.ParseThread(threadInfo);
            if (NS_FAILED(rv)) {
              continue;
            }
            info.threads.AppendElement(threadInfo);
          }

          if (!gathered.put(request.pid, std::move(info))) {
            holder->Reject(NS_ERROR_OUT_OF_MEMORY, __func__);
            return;
          }
        }

        // ... and we're done!
        holder->Resolve(std::move(gathered), __func__);
      });

  rv = target->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
  if (NS_FAILED(rv)) {
    NS_WARNING("Failed to dispatch the LoadDataRunnable.");
  }
  return promise;
}

}  // namespace mozilla