File: ProcInfo_bsd.cpp

package info (click to toggle)
firefox 142.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,591,884 kB
  • sloc: cpp: 7,451,570; javascript: 6,392,463; ansic: 3,712,584; python: 1,388,569; xml: 629,223; asm: 426,919; java: 184,857; sh: 63,439; makefile: 19,150; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (147 lines) | stat: -rw-r--r-- 4,197 bytes parent folder | download | duplicates (3)
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
/* -*- 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 "nsMemoryReporterManager.h"
#include "nsWhitespaceTokenizer.h"

#include <sys/types.h>
#include <sys/sysctl.h>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#ifdef __FreeBSD__
#  include <sys/user.h>
#endif

namespace mozilla {

int GetCycleTimeFrequencyMHz() { return 0; }

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;
  }

  return NS_ERROR_FAILURE;
}

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) {
    size_t size;
#ifdef __FreeBSD__
    constexpr int mibsize = 4;
    int mib[mibsize];
    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD;
    mib[3] = request.pid;
#else
    constexpr int mibsize = 6;
    int mib[mibsize];
    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID | KERN_PROC_SHOW_THREADS;
    mib[3] = request.pid;
    mib[4] = sizeof(kinfo_proc);
    mib[5] = 0;
#endif
    if (sysctl(mib, mibsize, nullptr, &size, nullptr, 0) == -1) {
      // Can't get info for this process. Skip it.
      continue;
    }

#ifdef __FreeBSD__
    auto procs = MakeUniqueFallible<kinfo_proc[]>(size / sizeof(kinfo_proc));
#else
    mib[5] = size / sizeof(kinfo_proc);
    auto procs = MakeUniqueFallible<kinfo_proc[]>(mib[5]);
#endif
    if (!procs) {
      result.SetReject(NS_ERROR_OUT_OF_MEMORY);
      return result;
    }
    if (sysctl(mib, mibsize, procs.get(), &size, nullptr, 0) == -1 &&
        errno != ENOMEM) {
      continue;
    }

    ProcInfo 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);

    bool found = false;
    for (size_t i = 0; i < size / sizeof(kinfo_proc); i++) {
      const auto& p = procs[i];
#ifdef __FreeBSD__
      if (i == 0) {
#else
      if (p.p_tid == -1) {
#endif
        // This is the process.
        found = true;
#ifdef __FreeBSD__
        info.cpuTime = uint64_t(p.ki_runtime) * 1'000u;
        info.memory = (p.ki_tsize + p.ki_dsize + p.ki_ssize) * getpagesize();
#else
        info.cpuTime = uint64_t(p.p_rtime_sec) * 1'000'000'000u +
                       uint64_t(p.p_rtime_usec) * 1'000u;
        info.memory =
            (p.p_vm_tsize + p.p_vm_dsize + p.p_vm_ssize) * getpagesize();
#endif

      } else {
        // This is one of its threads.
        ThreadInfo threadInfo;
#ifdef __FreeBSD__
        threadInfo.tid = p.ki_tid;
        threadInfo.cpuTime = uint64_t(p.ki_runtime) * 1'000u;
#else
        threadInfo.tid = p.p_tid;
        threadInfo.cpuTime = uint64_t(p.p_rtime_sec) * 1'000'000'000u +
                             uint64_t(p.p_rtime_usec) * 1'000u;
#endif
        info.threads.AppendElement(threadInfo);
      }
    }

    if (found && !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