14a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes/* 24a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * Copyright (C) 2013 The Android Open Source Project 34a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * 44a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 54a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * you may not use this file except in compliance with the License. 64a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * You may obtain a copy of the License at 74a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * 84a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 94a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * 104a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * Unless required by applicable law or agreed to in writing, software 114a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 124a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * See the License for the specific language governing permissions and 144a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes * limitations under the License. 154a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes */ 164a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes 174a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes#include "benchmark.h" 184a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes 193002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes#include <sys/syscall.h> 204a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes#include <time.h> 214a05bef4c06dac05f1c9aa8cfc5b7e7dd6642385Elliott Hughes 227634db5a0657129225869c3650a992f9cbe82fe4Elliott Hughesstatic void BM_time_clock_gettime(int iters) { 237634db5a0657129225869c3650a992f9cbe82fe4Elliott Hughes StartBenchmarkTiming(); 247634db5a0657129225869c3650a992f9cbe82fe4Elliott Hughes 253002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes timespec t; 267634db5a0657129225869c3650a992f9cbe82fe4Elliott Hughes for (int i = 0; i < iters; ++i) { 277634db5a0657129225869c3650a992f9cbe82fe4Elliott Hughes clock_gettime(CLOCK_MONOTONIC, &t); 287634db5a0657129225869c3650a992f9cbe82fe4Elliott Hughes } 297634db5a0657129225869c3650a992f9cbe82fe4Elliott Hughes 307634db5a0657129225869c3650a992f9cbe82fe4Elliott Hughes StopBenchmarkTiming(); 317634db5a0657129225869c3650a992f9cbe82fe4Elliott Hughes} 327634db5a0657129225869c3650a992f9cbe82fe4Elliott HughesBENCHMARK(BM_time_clock_gettime); 333002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 343002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughesstatic void BM_time_clock_gettime_syscall(int iters) { 353002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes StartBenchmarkTiming(); 363002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 373002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes timespec t; 383002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes for (int i = 0; i < iters; ++i) { 393002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t); 403002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes } 413002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 423002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes StopBenchmarkTiming(); 433002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes} 443002131da33401cf1b45abbdbec58b7c751fc43aElliott HughesBENCHMARK(BM_time_clock_gettime_syscall); 453002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 463002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughesstatic void BM_time_gettimeofday(int iters) { 473002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes StartBenchmarkTiming(); 483002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 493002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes timeval tv; 503002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes for (int i = 0; i < iters; ++i) { 513002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes gettimeofday(&tv, NULL); 523002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes } 533002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 543002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes StopBenchmarkTiming(); 553002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes} 563002131da33401cf1b45abbdbec58b7c751fc43aElliott HughesBENCHMARK(BM_time_gettimeofday); 573002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 583002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughesstatic void BM_time_gettimeofday_syscall(int iters) { 593002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes StartBenchmarkTiming(); 603002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 613002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes timeval tv; 623002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes for (int i = 0; i < iters; ++i) { 633002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes syscall(__NR_gettimeofday, &tv, NULL); 643002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes } 653002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 663002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes StopBenchmarkTiming(); 673002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes} 683002131da33401cf1b45abbdbec58b7c751fc43aElliott HughesBENCHMARK(BM_time_gettimeofday_syscall); 693002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 703002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughesstatic void BM_time_time(int iters) { 713002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes StartBenchmarkTiming(); 723002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 733002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes for (int i = 0; i < iters; ++i) { 743002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes time(NULL); 753002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes } 763002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes 773002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes StopBenchmarkTiming(); 783002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes} 793002131da33401cf1b45abbdbec58b7c751fc43aElliott HughesBENCHMARK(BM_time_time); 80