-
Notifications
You must be signed in to change notification settings - Fork 436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue=1251, use clock_gettime(CLOCK_MONOTONIC) to get current time #1252
Merged
+166
−54
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
49874c6
issue=1251, use clock_gettime(CLOCK_MONOTONIC) to get current time
069ca60
issue=1251, use clock_gettime(CLOCK_MONOTONIC) to get current time
c63531a
issue=1251, use clock_gettime(CLOCK_MONOTONIC) to get current time
251da41
issue=1251, use clock_gettime(CLOCK_MONOTONIC) to get current time
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include <pthread.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/time.h> | ||
#include <time.h> | ||
|
||
#include <functional> | ||
#include <iostream> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "common/mutex.h" | ||
#include "common/thread_pool.h" | ||
#include "common/timer.h" | ||
|
||
namespace tera { | ||
|
||
TEST(TimerTest, Basic) { | ||
struct timespec ts1, ts2, ts3; | ||
struct timeval tv; | ||
|
||
clock_gettime(CLOCK_MONOTONIC, &ts1); | ||
clock_gettime(CLOCK_MONOTONIC_RAW, &ts3); | ||
clock_gettime(CLOCK_REALTIME, &ts2); | ||
gettimeofday(&tv, NULL); | ||
|
||
std::cout << "ts1.tv_sec " << ts1.tv_sec | ||
<< ", ts1.tv_nsec " << ts1.tv_nsec | ||
<< std::endl; | ||
std::cout << "ts2.tv_sec " << ts2.tv_sec | ||
<< ", ts2.tv_nsec " << ts2.tv_nsec | ||
<< std::endl; | ||
std::cout << "ts3.tv_sec " << ts3.tv_sec | ||
<< ", ts3.tv_nsec " << ts3.tv_nsec | ||
<< std::endl; | ||
std::cout << "tv.tv_sec " << tv.tv_sec | ||
<< ", tv.tv_usec " << tv.tv_usec | ||
<< std::endl; | ||
|
||
int delta = 0; | ||
delta = ts2.tv_sec - tv.tv_sec; | ||
ASSERT_TRUE(-1 <= delta && delta <= 1); | ||
ASSERT_TRUE(ts1.tv_sec < ts2.tv_sec); | ||
ASSERT_TRUE(ts1.tv_sec < tv.tv_sec); | ||
} | ||
|
||
TEST(TimerTest, test1) { | ||
struct timespec ts1; | ||
struct timeval tv; | ||
|
||
clock_gettime(CLOCK_REALTIME, &ts1); | ||
gettimeofday(&tv, NULL); | ||
int64_t ts = common::timer::get_micros(); | ||
|
||
int delta = 0; | ||
delta = ts1.tv_sec - tv.tv_sec; | ||
ASSERT_TRUE(-1 <= delta && delta <= 1); | ||
delta = ts / 1000000 - tv.tv_sec; | ||
ASSERT_TRUE(-1 <= delta && delta <= 1); | ||
} | ||
|
||
common::Mutex mu; | ||
common::CondVar cv(&mu); | ||
|
||
void DelayTask_issue1(int32_t time, int32_t time_ms) { | ||
struct timespec ts1; | ||
clock_gettime(CLOCK_MONOTONIC, &ts1); | ||
int delta = ts1.tv_sec - (time + time_ms / 1000); | ||
ASSERT_TRUE(-1 <= delta && delta <= 1); | ||
cv.Signal(); | ||
return; | ||
} | ||
|
||
TEST(ThreadPoolTest, Basic) { | ||
mu.Lock(); | ||
common::ThreadPool* pool = new common::ThreadPool(1000); | ||
struct timespec ts1; | ||
clock_gettime(CLOCK_MONOTONIC, &ts1); | ||
ThreadPool::Task task = | ||
std::bind(&DelayTask_issue1, ts1.tv_sec, 5000); | ||
pool->DelayTask(5000, task); | ||
|
||
cv.Wait(); | ||
mu.Unlock(); | ||
delete pool; | ||
} | ||
|
||
} // namespace tera |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ts1是MONOTONIC的;
ts2是REALTIME的;
二者不是同一个东东,比较是啥意思。。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第一个断言,想说明gettimeofday和CLOCK_REALTIME两个函数获得的时间是一致的;
第二个断言,说明monotonic函数确实是取了系统启动后的时间,应该恒小于CLOCK_REALTIME