Bug 1933479 - Add tab close button on hover to vertical tabs when sidebar is collapse...
[gecko.git] / toolkit / components / processtools / ProcInfo_bsd.cpp
bloba6ff4881940cc07603f6733fb09b37768ef33dbf
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/ProcInfo.h"
8 #include "mozilla/Sprintf.h"
9 #include "mozilla/Logging.h"
10 #include "mozilla/ScopeExit.h"
11 #include "mozilla/ipc/GeckoChildProcessHost.h"
12 #include "nsMemoryReporterManager.h"
13 #include "nsWhitespaceTokenizer.h"
15 #include <sys/types.h>
16 #include <sys/sysctl.h>
17 #include <cerrno>
18 #include <cstdio>
19 #include <cstring>
20 #include <unistd.h>
22 namespace mozilla {
24 int GetCycleTimeFrequencyMHz() { return 0; }
26 nsresult GetCpuTimeSinceProcessStartInMs(uint64_t* aResult) {
27 timespec t;
28 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t) == 0) {
29 uint64_t cpuTime =
30 uint64_t(t.tv_sec) * 1'000'000'000u + uint64_t(t.tv_nsec);
31 *aResult = cpuTime / PR_NSEC_PER_MSEC;
32 return NS_OK;
35 return NS_ERROR_FAILURE;
38 nsresult GetGpuTimeSinceProcessStartInMs(uint64_t* aResult) {
39 return NS_ERROR_NOT_IMPLEMENTED;
42 ProcInfoPromise::ResolveOrRejectValue GetProcInfoSync(
43 nsTArray<ProcInfoRequest>&& aRequests) {
44 ProcInfoPromise::ResolveOrRejectValue result;
46 HashMap<base::ProcessId, ProcInfo> gathered;
47 if (!gathered.reserve(aRequests.Length())) {
48 result.SetReject(NS_ERROR_OUT_OF_MEMORY);
49 return result;
51 for (const auto& request : aRequests) {
52 size_t size;
53 int mib[6];
54 mib[0] = CTL_KERN;
55 mib[1] = KERN_PROC;
56 mib[2] = KERN_PROC_PID | KERN_PROC_SHOW_THREADS;
57 mib[3] = request.pid;
58 mib[4] = sizeof(kinfo_proc);
59 mib[5] = 0;
60 if (sysctl(mib, 6, nullptr, &size, nullptr, 0) == -1) {
61 // Can't get info for this process. Skip it.
62 continue;
65 mib[5] = size / sizeof(kinfo_proc);
66 auto procs = MakeUniqueFallible<kinfo_proc[]>(mib[5]);
67 if (!procs) {
68 result.SetReject(NS_ERROR_OUT_OF_MEMORY);
69 return result;
71 if (sysctl(mib, 6, procs.get(), &size, nullptr, 0) == -1 &&
72 errno != ENOMEM) {
73 continue;
76 ProcInfo info;
77 info.pid = request.pid;
78 info.childId = request.childId;
79 info.type = request.processType;
80 info.origin = request.origin;
81 info.windows = std::move(request.windowInfo);
82 info.utilityActors = std::move(request.utilityInfo);
84 bool found = false;
85 for (size_t i = 0; i < size / sizeof(kinfo_proc); i++) {
86 const auto& p = procs[i];
87 if (p.p_tid == -1) {
88 // This is the process.
89 found = true;
90 info.cpuTime = uint64_t(p.p_rtime_sec) * 1'000'000'000u +
91 uint64_t(p.p_rtime_usec) * 1'000u;
92 info.memory =
93 (p.p_vm_tsize + p.p_vm_dsize + p.p_vm_ssize) * getpagesize();
94 } else {
95 // This is one of its threads.
96 ThreadInfo threadInfo;
97 threadInfo.tid = p.p_tid;
98 threadInfo.cpuTime = uint64_t(p.p_rtime_sec) * 1'000'000'000u +
99 uint64_t(p.p_rtime_usec) * 1'000u;
100 info.threads.AppendElement(threadInfo);
104 if (found && !gathered.put(request.pid, std::move(info))) {
105 result.SetReject(NS_ERROR_OUT_OF_MEMORY);
106 return result;
110 // ... and we're done!
111 result.SetResolve(std::move(gathered));
112 return result;
115 } // namespace mozilla