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>
24 int GetCycleTimeFrequencyMHz() { return 0; }
26 nsresult
GetCpuTimeSinceProcessStartInMs(uint64_t* aResult
) {
28 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID
, &t
) == 0) {
30 uint64_t(t
.tv_sec
) * 1'000'000'000u + uint64_t(t
.tv_nsec
);
31 *aResult
= cpuTime
/ PR_NSEC_PER_MSEC
;
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
);
51 for (const auto& request
: aRequests
) {
56 mib
[2] = KERN_PROC_PID
| KERN_PROC_SHOW_THREADS
;
58 mib
[4] = sizeof(kinfo_proc
);
60 if (sysctl(mib
, 6, nullptr, &size
, nullptr, 0) == -1) {
61 // Can't get info for this process. Skip it.
65 mib
[5] = size
/ sizeof(kinfo_proc
);
66 auto procs
= MakeUniqueFallible
<kinfo_proc
[]>(mib
[5]);
68 result
.SetReject(NS_ERROR_OUT_OF_MEMORY
);
71 if (sysctl(mib
, 6, procs
.get(), &size
, nullptr, 0) == -1 &&
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
);
85 for (size_t i
= 0; i
< size
/ sizeof(kinfo_proc
); i
++) {
86 const auto& p
= procs
[i
];
88 // This is the process.
90 info
.cpuTime
= uint64_t(p
.p_rtime_sec
) * 1'000'000'000u +
91 uint64_t(p
.p_rtime_usec
) * 1'000u;
93 (p
.p_vm_tsize
+ p
.p_vm_dsize
+ p
.p_vm_ssize
) * getpagesize();
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
);
110 // ... and we're done!
111 result
.SetResolve(std::move(gathered
));
115 } // namespace mozilla