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:
@@ -1,7 +1,7 @@
|
|||||||
#ifndef WVCDM_METRICS_TIMER_METRIC_H_
|
#ifndef WVCDM_METRICS_TIMER_METRIC_H_
|
||||||
#define WVCDM_METRICS_TIMER_METRIC_H_
|
#define WVCDM_METRICS_TIMER_METRIC_H_
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <chrono>
|
||||||
|
|
||||||
namespace wvcdm {
|
namespace wvcdm {
|
||||||
namespace metrics {
|
namespace metrics {
|
||||||
@@ -23,8 +23,8 @@ class TimerMetric {
|
|||||||
double AsUs() const;
|
double AsUs() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double sec_;
|
std::chrono::steady_clock clock_;
|
||||||
double usec_;
|
std::chrono::time_point<std::chrono::steady_clock> start_;
|
||||||
bool is_started_;
|
bool is_started_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,39 +1,23 @@
|
|||||||
#include "timer_metric.h"
|
#include "timer_metric.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
namespace wvcdm {
|
namespace wvcdm {
|
||||||
namespace metrics {
|
namespace metrics {
|
||||||
|
|
||||||
void TimerMetric::Start() {
|
void TimerMetric::Start() {
|
||||||
struct timeval tv;
|
start_ = clock_.now();
|
||||||
gettimeofday(&tv, NULL);
|
|
||||||
sec_ = tv.tv_sec;
|
|
||||||
usec_ = tv.tv_usec;
|
|
||||||
is_started_ = true;
|
is_started_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimerMetric::Clear() {
|
void TimerMetric::Clear() {
|
||||||
is_started_ = false;
|
is_started_ = false;
|
||||||
sec_ = 0;
|
|
||||||
usec_ = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double TimerMetric::AsMs() const {
|
double TimerMetric::AsMs() const {
|
||||||
struct timeval tv;
|
return (clock_.now() - start_) / std::chrono::milliseconds(1);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double TimerMetric::AsUs() const {
|
double TimerMetric::AsUs() const {
|
||||||
struct timeval tv;
|
return (clock_.now() - start_) / std::chrono::microseconds(1);
|
||||||
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_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace metrics
|
} // namespace metrics
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <chrono>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -90,14 +91,8 @@ SessionContext* CryptoEngine::FindSession(SessionId sid) {
|
|||||||
time_t CryptoEngine::OnlineTime() {
|
time_t CryptoEngine::OnlineTime() {
|
||||||
// Use the monotonic clock for times that don't have to be stable across
|
// Use the monotonic clock for times that don't have to be stable across
|
||||||
// device boots.
|
// device boots.
|
||||||
timespec current_time;
|
std::chrono::steady_clock clock;
|
||||||
int gettime_result = clock_gettime(CLOCK_MONOTONIC, ¤t_time);
|
return clock.now().time_since_epoch() / std::chrono::seconds(1);
|
||||||
if (gettime_result == 0) {
|
|
||||||
return current_time.tv_sec;
|
|
||||||
} else {
|
|
||||||
// Can't use monotonic clock, use roll back time.
|
|
||||||
return RollbackCorrectedOfflineTime();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
time_t CryptoEngine::RollbackCorrectedOfflineTime() {
|
time_t CryptoEngine::RollbackCorrectedOfflineTime() {
|
||||||
|
|||||||
@@ -15,8 +15,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/time.h>
|
#include <chrono>
|
||||||
#include <time.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@@ -162,12 +161,6 @@ OEMCRYPTO_API OEMCryptoResult OEMCrypto_GenerateDerivedKeys(
|
|||||||
return OEMCrypto_SUCCESS;
|
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,
|
OEMCRYPTO_API OEMCryptoResult OEMCrypto_GenerateNonce(OEMCrypto_SESSION session,
|
||||||
uint32_t* nonce) {
|
uint32_t* nonce) {
|
||||||
@@ -182,13 +175,14 @@ OEMCRYPTO_API OEMCryptoResult OEMCrypto_GenerateNonce(OEMCrypto_SESSION session,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prevent nonce flood.
|
// Prevent nonce flood.
|
||||||
uint64_t now = TimeStamp();
|
static std::chrono::steady_clock clock;
|
||||||
static uint64_t last_nonce_time = now;
|
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
|
// 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.
|
// 1, the very first nonce after initialization is counted as a flood.
|
||||||
static int nonce_count = 1;
|
static int nonce_count = 1;
|
||||||
|
|
||||||
if (now - last_nonce_time < one_second) {
|
if (now - last_nonce_time < std::chrono::seconds(1)) {
|
||||||
nonce_count++;
|
nonce_count++;
|
||||||
if (nonce_count > crypto_engine->nonce_flood_count()) {
|
if (nonce_count > crypto_engine->nonce_flood_count()) {
|
||||||
LOGE("[OEMCrypto_GenerateNonce(): Nonce Flood detected]");
|
LOGE("[OEMCrypto_GenerateNonce(): Nonce Flood detected]");
|
||||||
|
|||||||
@@ -16,11 +16,11 @@
|
|||||||
#include <openssl/x509.h>
|
#include <openssl/x509.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -2818,32 +2818,25 @@ class OEMCryptoUsesCertificate : public OEMCryptoLoadsCertificate {
|
|||||||
// This test is not run by default, because it takes a long time and
|
// This test is not run by default, because it takes a long time and
|
||||||
// is used to measure RSA performance, not test functionality.
|
// is used to measure RSA performance, not test functionality.
|
||||||
TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
|
TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
|
||||||
|
const std::chrono::milliseconds kTestDuration(5000);
|
||||||
OEMCryptoResult sts;
|
OEMCryptoResult sts;
|
||||||
|
std::chrono::steady_clock clock;
|
||||||
sleep(2); // Make sure are not nonce limited.
|
sleep(2); // Make sure are not nonce limited.
|
||||||
|
|
||||||
const uint32_t TestDuration = 5000; // milliseconds.
|
auto start_time = clock.now();
|
||||||
struct timeval start_time, end_time;
|
int count = 15;
|
||||||
gettimeofday(&start_time, NULL);
|
for (int i = 0; i < count; i++) { // Only 20 nonce available.
|
||||||
gettimeofday(&end_time, NULL);
|
|
||||||
double mtime = 0;
|
|
||||||
long count = 0;
|
|
||||||
for (int i = 0; i < 15; i++) { // Only 20 nonce available.
|
|
||||||
CreateWrappedRSAKey(kSign_RSASSA_PSS, true);
|
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;
|
Session session;
|
||||||
CreateWrappedRSAKey(kSign_RSASSA_PSS, true);
|
CreateWrappedRSAKey(kSign_RSASSA_PSS, true);
|
||||||
gettimeofday(&start_time, NULL);
|
start_time = clock.now();
|
||||||
gettimeofday(&end_time, NULL);
|
|
||||||
mtime = 0;
|
|
||||||
count = 0;
|
count = 0;
|
||||||
do {
|
while (clock.now() - start_time < kTestDuration) {
|
||||||
Session s;
|
Session s;
|
||||||
ASSERT_NO_FATAL_FAILURE(s.open());
|
ASSERT_NO_FATAL_FAILURE(s.open());
|
||||||
sts = OEMCrypto_LoadDeviceRSAKey(s.session_id(), wrapped_rsa_key_.data(),
|
sts = OEMCrypto_LoadDeviceRSAKey(s.session_id(), wrapped_rsa_key_.data(),
|
||||||
@@ -2865,12 +2858,10 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
|
|||||||
delete[] signature;
|
delete[] signature;
|
||||||
ASSERT_EQ(OEMCrypto_SUCCESS, sts);
|
ASSERT_EQ(OEMCrypto_SUCCESS, sts);
|
||||||
count++;
|
count++;
|
||||||
gettimeofday(&end_time, NULL);
|
}
|
||||||
long seconds = end_time.tv_sec - start_time.tv_sec;
|
delta_time = clock.now() - start_time;
|
||||||
long useconds = end_time.tv_usec - start_time.tv_usec;
|
const double license_request_time =
|
||||||
mtime = seconds * 1e3 + useconds * 1e-3;
|
delta_time / std::chrono::milliseconds(1) / count;
|
||||||
} while (mtime < TestDuration);
|
|
||||||
double license_request_time = mtime / count;
|
|
||||||
|
|
||||||
Session s;
|
Session s;
|
||||||
ASSERT_NO_FATAL_FAILURE(s.open());
|
ASSERT_NO_FATAL_FAILURE(s.open());
|
||||||
@@ -2885,10 +2876,6 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
|
|||||||
vector<uint8_t> mac_context;
|
vector<uint8_t> mac_context;
|
||||||
vector<uint8_t> enc_context;
|
vector<uint8_t> enc_context;
|
||||||
s.FillDefaultContext(&mac_context, &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(
|
enc_session_key = wvcdm::a2b_hex(
|
||||||
"7789c619aa3b9fa3c0a53f57a4abc6"
|
"7789c619aa3b9fa3c0a53f57a4abc6"
|
||||||
@@ -2908,7 +2895,8 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
|
|||||||
"ad2b1254f80c0c5dd3cf111b56572217"
|
"ad2b1254f80c0c5dd3cf111b56572217"
|
||||||
"b9f58fc1dacbf74b59d354a1e62cfa0e"
|
"b9f58fc1dacbf74b59d354a1e62cfa0e"
|
||||||
"bf");
|
"bf");
|
||||||
do {
|
start_time = clock.now();
|
||||||
|
while (clock.now() - start_time < kTestDuration) {
|
||||||
ASSERT_EQ(OEMCrypto_SUCCESS,
|
ASSERT_EQ(OEMCrypto_SUCCESS,
|
||||||
OEMCrypto_DeriveKeysFromSessionKey(
|
OEMCrypto_DeriveKeysFromSessionKey(
|
||||||
s.session_id(),
|
s.session_id(),
|
||||||
@@ -2916,12 +2904,10 @@ TEST_F(OEMCryptoLoadsCertificate, RSAPerformance) {
|
|||||||
mac_context.data(), mac_context.size(),
|
mac_context.data(), mac_context.size(),
|
||||||
enc_context.data(), enc_context.size()));
|
enc_context.data(), enc_context.size()));
|
||||||
count++;
|
count++;
|
||||||
gettimeofday(&end_time, NULL);
|
}
|
||||||
long seconds = end_time.tv_sec - start_time.tv_sec;
|
delta_time = clock.now() - start_time;
|
||||||
long useconds = end_time.tv_usec - start_time.tv_usec;
|
const double derive_keys_time =
|
||||||
mtime = seconds * 1e3 + useconds * 1e-3;
|
delta_time / std::chrono::milliseconds(1) / count;
|
||||||
} while (mtime < TestDuration);
|
|
||||||
double derive_keys_time = mtime / count;
|
|
||||||
|
|
||||||
const char* level = OEMCrypto_SecurityLevel();
|
const char* level = OEMCrypto_SecurityLevel();
|
||||||
printf("PERF:head, security, provision (ms), lic req(ms), derive keys(ms)\n");
|
printf("PERF:head, security, provision (ms), lic req(ms), derive keys(ms)\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user