Use the std::chrono to get the time.

[ Merge of http://go/wvgerrit/67985 ]

Now that we can use C++11, we should use the cross-platform types for
clocks instead of the platform-specific versions.

Test: WV unit/integration tests.
Change-Id: I50318e3d1caf9e814f33f497f83c19c9f3c154a1
This commit is contained in:
Rahul Frias
2018-12-13 12:09:04 -08:00
parent 3c350b677f
commit 25d29fd22b
5 changed files with 35 additions and 76 deletions

View File

@@ -1,7 +1,7 @@
#ifndef WVCDM_METRICS_TIMER_METRIC_H_
#define WVCDM_METRICS_TIMER_METRIC_H_
#include <stdint.h>
#include <chrono>
namespace wvcdm {
namespace metrics {
@@ -23,8 +23,8 @@ class TimerMetric {
double AsUs() const;
private:
double sec_;
double usec_;
std::chrono::steady_clock clock_;
std::chrono::time_point<std::chrono::steady_clock> start_;
bool is_started_;
};

View File

@@ -1,39 +1,23 @@
#include "timer_metric.h"
#include <stddef.h>
#include <sys/time.h>
namespace wvcdm {
namespace metrics {
void TimerMetric::Start() {
struct timeval tv;
gettimeofday(&tv, NULL);
sec_ = tv.tv_sec;
usec_ = tv.tv_usec;
start_ = clock_.now();
is_started_ = true;
}
void TimerMetric::Clear() {
is_started_ = false;
sec_ = 0;
usec_ = 0;
}
double TimerMetric::AsMs() const {
struct timeval tv;
gettimeofday(&tv, NULL);
return usec_ > tv.tv_usec ?
(tv.tv_sec - sec_ - 1) * 1000.0 + (tv.tv_usec - usec_ + 1000000.0) / 1000.0 :
(tv.tv_sec - sec_) * 1000.0 + (tv.tv_usec - usec_) / 1000.0;
return (clock_.now() - start_) / std::chrono::milliseconds(1);
}
double TimerMetric::AsUs() const {
struct timeval tv;
gettimeofday(&tv, NULL);
return usec_ > tv.tv_usec ?
(tv.tv_sec - sec_ - 1) * 1000000.0 + (tv.tv_usec - usec_ + 1000000.0) :
(tv.tv_sec - sec_) * 1000000.0 + (tv.tv_usec - usec_);
return (clock_.now() - start_) / std::chrono::microseconds(1);
}
} // namespace metrics

View File

@@ -8,6 +8,7 @@
#include <arpa/inet.h>
#include <assert.h>
#include <chrono>
#include <string.h>
#include <algorithm>
#include <iostream>
@@ -90,14 +91,8 @@ SessionContext* CryptoEngine::FindSession(SessionId sid) {
time_t CryptoEngine::OnlineTime() {
// Use the monotonic clock for times that don't have to be stable across
// device boots.
timespec current_time;
int gettime_result = clock_gettime(CLOCK_MONOTONIC, &current_time);
if (gettime_result == 0) {
return current_time.tv_sec;
} else {
// Can't use monotonic clock, use roll back time.
return RollbackCorrectedOfflineTime();
}
std::chrono::steady_clock clock;
return clock.now().time_since_epoch() / std::chrono::seconds(1);
}
time_t CryptoEngine::RollbackCorrectedOfflineTime() {

View File

@@ -15,8 +15,7 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <chrono>
#include <iostream>
#include <string>
#include <utility>
@@ -162,12 +161,6 @@ OEMCRYPTO_API OEMCryptoResult OEMCrypto_GenerateDerivedKeys(
return OEMCrypto_SUCCESS;
}
static const uint64_t one_second = 1000000ull;
static uint64_t TimeStamp(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec * one_second + tv.tv_usec;
}
OEMCRYPTO_API OEMCryptoResult OEMCrypto_GenerateNonce(OEMCrypto_SESSION session,
uint32_t* nonce) {
@@ -182,13 +175,14 @@ OEMCRYPTO_API OEMCryptoResult OEMCrypto_GenerateNonce(OEMCrypto_SESSION session,
}
// Prevent nonce flood.
uint64_t now = TimeStamp();
static uint64_t last_nonce_time = now;
static std::chrono::steady_clock clock;
const auto now = clock.now().time_since_epoch();
static auto last_nonce_time = now;
// For testing, we set nonce_flood_count to 1. Since count is initialized to
// 1, the very first nonce after initialization is counted as a flood.
static int nonce_count = 1;
if (now - last_nonce_time < one_second) {
if (now - last_nonce_time < std::chrono::seconds(1)) {
nonce_count++;
if (nonce_count > crypto_engine->nonce_flood_count()) {
LOGE("[OEMCrypto_GenerateNonce(): Nonce Flood detected]");

View File

@@ -16,11 +16,11 @@
#include <openssl/x509.h>
#include <stdint.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <map>
#include <string>
@@ -2818,32 +2818,25 @@ class OEMCryptoUsesCertificate : public OEMCryptoLoadsCertificate {
// This test is not run by default, because it takes a long time and
// is used to measure RSA performance, not test functionality.
TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
const std::chrono::milliseconds kTestDuration(5000);
OEMCryptoResult sts;
std::chrono::steady_clock clock;
sleep(2); // Make sure are not nonce limited.
const uint32_t TestDuration = 5000; // milliseconds.
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
gettimeofday(&end_time, NULL);
double mtime = 0;
long count = 0;
for (int i = 0; i < 15; i++) { // Only 20 nonce available.
auto start_time = clock.now();
int count = 15;
for (int i = 0; i < count; i++) { // Only 20 nonce available.
CreateWrappedRSAKey(kSign_RSASSA_PSS, true);
count++;
gettimeofday(&end_time, NULL);
long seconds = end_time.tv_sec - start_time.tv_sec;
long useconds = end_time.tv_usec - start_time.tv_usec;
mtime = seconds * 1e3 + useconds * 1e-3;
}
double provision_time = mtime / count;
auto delta_time = clock.now() - start_time;
const double provision_time =
delta_time / std::chrono::milliseconds(1) / count;
Session session;
CreateWrappedRSAKey(kSign_RSASSA_PSS, true);
gettimeofday(&start_time, NULL);
gettimeofday(&end_time, NULL);
mtime = 0;
start_time = clock.now();
count = 0;
do {
while (clock.now() - start_time < kTestDuration) {
Session s;
ASSERT_NO_FATAL_FAILURE(s.open());
sts = OEMCrypto_LoadDeviceRSAKey(s.session_id(), wrapped_rsa_key_.data(),
@@ -2865,12 +2858,10 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
delete[] signature;
ASSERT_EQ(OEMCrypto_SUCCESS, sts);
count++;
gettimeofday(&end_time, NULL);
long seconds = end_time.tv_sec - start_time.tv_sec;
long useconds = end_time.tv_usec - start_time.tv_usec;
mtime = seconds * 1e3 + useconds * 1e-3;
} while (mtime < TestDuration);
double license_request_time = mtime / count;
}
delta_time = clock.now() - start_time;
const double license_request_time =
delta_time / std::chrono::milliseconds(1) / count;
Session s;
ASSERT_NO_FATAL_FAILURE(s.open());
@@ -2885,10 +2876,6 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
vector<uint8_t> mac_context;
vector<uint8_t> enc_context;
s.FillDefaultContext(&mac_context, &enc_context);
gettimeofday(&start_time, NULL);
gettimeofday(&end_time, NULL);
mtime = 0;
count = 0;
enc_session_key = wvcdm::a2b_hex(
"7789c619aa3b9fa3c0a53f57a4abc6"
@@ -2908,7 +2895,8 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
"ad2b1254f80c0c5dd3cf111b56572217"
"b9f58fc1dacbf74b59d354a1e62cfa0e"
"bf");
do {
start_time = clock.now();
while (clock.now() - start_time < kTestDuration) {
ASSERT_EQ(OEMCrypto_SUCCESS,
OEMCrypto_DeriveKeysFromSessionKey(
s.session_id(),
@@ -2916,12 +2904,10 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
mac_context.data(), mac_context.size(),
enc_context.data(), enc_context.size()));
count++;
gettimeofday(&end_time, NULL);
long seconds = end_time.tv_sec - start_time.tv_sec;
long useconds = end_time.tv_usec - start_time.tv_usec;
mtime = seconds * 1e3 + useconds * 1e-3;
} while (mtime < TestDuration);
double derive_keys_time = mtime / count;
}
delta_time = clock.now() - start_time;
const double derive_keys_time =
delta_time / std::chrono::milliseconds(1) / count;
const char* level = OEMCrypto_SecurityLevel();
printf("PERF:head, security, provision (ms), lic req(ms), derive keys(ms)\n");