[macOS] Do not crash if there is an attempt to copy a file URL to the clipboard
[WebKit-https.git] / Source / WTF / wtf / cocoa / CPUTimeCocoa.cpp
1 /*
2  * Copyright (C) 2017-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "CPUTime.h"
28
29 #import "MachSendRight.h"
30 #import <mach/mach.h>
31 #import <mach/mach_time.h>
32 #import <mach/task.h>
33 #import <mach/task_info.h>
34 #import <mach/thread_info.h>
35 #import <sys/time.h>
36
37 namespace WTF {
38
39 static const int64_t microsecondsPerSecond = 1000000;
40
41 static int64_t timeValueToMicroseconds(const time_value_t& value)
42 {
43     int64_t result = value.seconds;
44     result *= microsecondsPerSecond;
45     result += value.microseconds;
46     return result;
47 }
48
49 std::optional<CPUTime> CPUTime::get()
50 {
51     // Account for current threads.
52     task_thread_times_info threadInfoData;
53     mach_msg_type_number_t threadInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
54     kern_return_t result = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, reinterpret_cast<task_info_t>(&threadInfoData), &threadInfoCount);
55     if (result != KERN_SUCCESS)
56         return std::nullopt;
57
58     int64_t userTime = timeValueToMicroseconds(threadInfoData.user_time);
59     int64_t systemTime = timeValueToMicroseconds(threadInfoData.system_time);
60
61     // Account for termined threads.
62     task_basic_info taskInfoData;
63     mach_msg_type_number_t taskInfoCount = TASK_BASIC_INFO_COUNT;
64     result = task_info(mach_task_self(), TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&taskInfoData), &taskInfoCount);
65     if (result != KERN_SUCCESS)
66         return std::nullopt;
67
68     userTime += timeValueToMicroseconds(taskInfoData.user_time);
69     systemTime += timeValueToMicroseconds(taskInfoData.system_time);
70
71     return CPUTime { MonotonicTime::now(), Seconds::fromMicroseconds(userTime), Seconds::fromMicroseconds(systemTime) };
72 }
73
74 Seconds CPUTime::forCurrentThread()
75 {
76     mach_msg_type_number_t infoCount = THREAD_BASIC_INFO_COUNT;
77     thread_basic_info_data_t info;
78
79     auto threadPort = MachSendRight::adopt(mach_thread_self());
80     auto ret = thread_info(threadPort.sendRight(), THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&info), &infoCount);
81     RELEASE_ASSERT(ret == KERN_SUCCESS);
82
83     return Seconds(info.user_time.seconds + info.system_time.seconds) + Seconds::fromMicroseconds(info.user_time.microseconds + info.system_time.microseconds);
84 }
85
86 }