OEMCrypto Profiler

This CL is a merge from the widevine repo of:
http://go/wvgerrit/16491 Circular Buffer
http://go/wvgerrit/16512 Circular Buffer Tests
http://go/wvgerrit/16493 Entry Writer
http://go/wvgerrit/16495 Profiled Scope
http://go/wvgerrit/16500 Stats Collection
http://go/wvgerrit/16543 Disallow Stats Copy or Assign
http://go/wvgerrit/16514 Moving OEM Function Enum
http://go/wvgerrit/16501 Defining Session Interface
http://go/wvgerrit/16502 Session Definitions
http://go/wvgerrit/16573 Remove code to num bytes table
http://go/wvgerrit/16556 Connecting Profiler to Profiled Scope
http://go/wvgerrit/16557 Android Reading Profiler History
http://go/wvgerrit/16574 Adding Get Stats Method
http://go/wvgerrit/16606 Seperating Session Parsing
http://go/wvgerrit/16607 Adding get stats method to DRMPlugin
http://go/wvgerrit/16608 Fixing Linux Build Failure
http://go/wvgerrit/16612 Stop Clearing History
http://go/wvgerrit/16613 Accessing profiler information using session id
http://go/wvgerrit/16614 Making All Session Subsets of Global Session

BUG: 25123303
BUG: 26027857
Change-Id: Ie2422e644aa631871852ea0e461695aeb7060f88
This commit is contained in:
Aaron Vaage
2016-01-27 10:14:46 -08:00
parent 1d805385ce
commit a249c67504
23 changed files with 1608 additions and 2 deletions

View File

@@ -0,0 +1,62 @@
// Copyright 2016 Google Inc. All Rights Reserved.
#ifndef WVCDM_PROFILER_CIRCULAR_BUFFER_H_
#define WVCDM_PROFILER_CIRCULAR_BUFFER_H_
#include <stddef.h>
#include <stdint.h>
#include <vector>
namespace wvcdm {
namespace oemprofiler {
class CircularBuffer {
public:
explicit CircularBuffer(size_t memoryBudget);
size_t GetFreeSpace() const;
size_t GetUsedSpace() const;
// Add a series of bytes to the buffer. Values will remain in
// the buffer until Remove removes them. If there is not enough
// room for the full series, no bytes are written into the buffer.
bool AddU8s(const uint8_t* values, size_t numValues);
// Add a single byte to the buffer. The value will remain in the
// buffer until Remove removes it. If the buffer is full, the
// byte will not be written into the buffer.
bool AddU8(uint8_t value);
// Read a series of bytes without removing them. The start of the
// read is from the end (the oldest entry) + offset (toward the newest
// entry). If requested number of bytes is too large, no bytes are read
// and the function returns false.
bool PeekU8s(size_t offset, uint8_t* out, size_t numToRead) const;
// Read a single byte without removing it. The read byte is at the index
// of the oldest entry + offset (toward the newest entry). If there are
// no bytes in the buffer the function returns false.
bool PeekU8(size_t offset, uint8_t* out) const;
// Remove the requested number of bytes from the buffer from oldest
// to newest. If there are not enough bytes to remove, nothing is
// removed and the function returns false.
bool Remove(size_t numBytes);
private:
std::vector<uint8_t> buffer_;
size_t fill_;
size_t head_;
size_t tail_;
// disallow copy and assign
CircularBuffer(const CircularBuffer&);
void operator=(const CircularBuffer&);
};
} // namespace oemprofiler
} // namespace wvcdm
#endif

View File

@@ -0,0 +1,71 @@
// Copyright 2016 Google Inc. All Rights Reserved.
#ifndef WVCDM_PROFILER_ENTRY_WRITER_H_
#define WVCDM_PROFILER_ENTRY_WRITER_H_
#include <stddef.h>
#include <stdint.h>
namespace wvcdm {
namespace oemprofiler {
class EntryWriter {
public:
explicit EntryWriter();
const uint8_t* GetData() const;
size_t GetSize() const;
int WriteU8(uint8_t value);
int WriteU16(uint16_t value);
int WriteU32(uint32_t value);
int WriteU64(uint64_t value);
// WriteVLV
// Write a uint 64 value using a variable number of bytes. The number
// of bytes used is based on the number of bytes required to
// express the value.
//
// The first 3 bits are used to express the number of bytes
// (ranging 1 to 8 bytes).
//
// 1 byte = 0 to 31
// 2 bytes = 32 to 8191
// 3 bytes = 8192 to 2097151
// 4 bytes = 2097152 to 536870911
// 5 bytes = 536870912 to 137438953471
// 6 bytes = 137438953472 to 35184372088831
// 7 bytes = 35184372088832 to 9007199254740991
// 8 bytes = 9007199254740992 to 2305843009213693951
int WriteVLV(uint64_t value);
void Clear();
size_t GetFreeSpace() const;
private:
static const size_t kBufferSize = 32;
uint8_t bytes_[kBufferSize];
size_t write_head_;
int Write(uint64_t v, size_t num_bytes);
template <typename T>
int Write(T v);
uint8_t GetByte(uint64_t value, size_t byte_index);
// disallow copy and assign
EntryWriter(const EntryWriter&);
void operator=(const EntryWriter&);
};
} // oemprofiler
} // wvcdm
#endif

View File

@@ -0,0 +1,59 @@
// Copyright 2016 Google Inc. All Rights Reserved.
#ifndef WVCDM_PROFILER_OEM_FUNCTIONS_H_
#define WVCDM_PROFILER_OEM_FUNCTIONS_H_
namespace wvcdm {
namespace oemprofiler {
enum OEM_FUNCTION {
OEM_FUNCTION_OPEN_SESSION = 0,
OEM_FUNCTION_COPY_BUFFER,
OEM_FUNCTION_INSTALL_KEYBOX,
OEM_FUNCTION_IS_KEYBOX_VALID,
OEM_FUNCTION_GET_DEVICE_ID,
OEM_FUNCTION_GET_KEY_DATA,
OEM_FUNCTION_API_VERSION,
OEM_FUNCTION_SECURITY_PATCH_LEVEL,
OEM_FUNCTION_SECURITY_LEVEL,
OEM_FUNCTION_GET_HDCP_CAPABILITIY,
OEM_FUNCTION_SUPPORTS_USAGE_TABLE,
OEM_FUNCTION_IS_ANTI_ROLLBACK_HW_PRESENT,
OEM_FUNCTION_GET_NUMBER_OF_OPEN_SESSIONS,
OEM_FUNCTION_GET_MAX_NUMBER_OF_SESSIONS,
OEM_FUNCTION_CLOSE_SESSION,
OEM_FUNCTION_GENERATE_DERIVED_KEYS,
OEM_FUNCTION_GENERATE_NONCE,
OEM_FUNCTION_GENERATE_SIGNATURE,
OEM_FUNCTION_LOAD_KEYS,
OEM_FUNCTION_REFRESH_KEYS,
OEM_FUNCTION_QUERY_KEY_CONTROL,
OEM_FUNCTION_SELECT_KEY,
OEM_FUNCTION_DECRYPT_CENC,
OEM_FUNCTION_WRAP_KEYBOX,
OEM_FUNCTION_LOAD_TEST_KEYBOX,
OEM_FUNCTION_GET_RANDOM,
OEM_FUNCTION_REWRAP_DEVICE_RSA_KEY,
OEM_FUNCTION_LOAD_DEVICE_RSA_KEY,
OEM_FUNCTION_LOAD_TEST_RSA_KEY,
OEM_FUNCTION_GENERATE_RSA_SIGNATURE,
OEM_FUNCTION_DERIVE_KEYS_FROM_SESSION_KEY,
OEM_FUNCTION_GENERIC_ENCRYPT,
OEM_FUNCTION_GENERIC_DECRYPT,
OEM_FUNCTION_GENERIC_SIGN,
OEM_FUNCTION_GENERIC_VERIFY,
OEM_FUNCTION_UPDATE_USAGE_TABLE,
OEM_FUNCTION_DEACTIVATE_USAGE_ENTRY,
OEM_FUNCTION_REPORT_USAGE,
OEM_FUNCTION_DELETE_USAGE_ENTRY,
OEM_FUNCTION_FORCE_DELETE_USAGE_ENTRY,
OEM_FUNCTION_DELETE_USAGE_TABLE,
OEM_FUNCTION_TESTING, // dummy value for testing purposes
OEM_FUNCTION_COUNT
};
} // namespace oemprofiler
} // namespace wvcdm
#endif

View File

@@ -0,0 +1,46 @@
// Copyright 2016 Google Inc. All Rights Reserved.
#ifndef WVCDM_PROFILER_PROFILED_SCOPE_H_
#define WVCDM_PROFILER_PROFILED_SCOPE_H_
#include <stdint.h>
#include "entry_writer.h"
#include "oem_functions.h"
namespace wvcdm {
namespace oemprofiler {
class ProfiledScope {
public:
explicit ProfiledScope(OEM_FUNCTION fid);
explicit ProfiledScope(uint32_t sid, OEM_FUNCTION fid);
~ProfiledScope();
EntryWriter meta_data_;
// All profiling data must be assigned to a session but some oem
// crypto calls are not associated with crypto sessions. To gather
// those functions' data, the global session id is used. Crypto.Session
// Ids are unsigned 32 bit integers. To use those as a profiling session
// id but also allow for a global id that won't conflict with them,
// we use a -1 as the a proifiling session id is a signed 64 bit integer.
static const int64_t kGlobalSID = -1;
private:
int64_t sid_;
OEM_FUNCTION fid_;
uint64_t start_time_;
void Submit(int64_t sid, uint64_t end_time) const;
uint64_t GetNowUS() const;
// disallow copy and assign
ProfiledScope(const ProfiledScope&);
void operator=(const ProfiledScope&);
};
} // namespace oemprofiler
} // namespace wvcdm
#endif

View File

@@ -0,0 +1,73 @@
// Copyright 2016 Google Inc. All Rights Reserved.
#ifndef WVCDM_PROFILER_SESSION_H_
#define WVCDM_PROFILER_SESSION_H_
#include <map>
#include <stddef.h>
#include <stdint.h>
#include <vector>
#include "circular_buffer.h"
#include "entry_writer.h"
#include "oem_functions.h"
#include "stats.h"
namespace wvcdm {
namespace oemprofiler {
class ProfilerSession {
public:
ProfilerSession();
void Submit(
OEM_FUNCTION fid,
uint64_t start_time,
uint64_t end_time,
const uint8_t* meta_data,
size_t meta_data_length);
// clear all samples and stats
void Clear();
void ReadHistory(std::vector<uint8_t>& output) const;
void ReadAllStats(std::vector<uint8_t>& output) const;
const Stat& ReadStat(OEM_FUNCTION fid) const;
static void Open(int64_t sid);
static void Close(int64_t sid);
static ProfilerSession* Find(int64_t sid);
private:
Stat stats_[OEM_FUNCTION_COUNT];
CircularBuffer buffer_;
uint64_t time_at_head_;
uint64_t time_at_tail_;
bool RequestSpace(uint8_t num_bytes);
bool ReadNextEntryRealEndTime(uint64_t* output);
bool DropLastEntry();
// Read a variable length value. This is the read that matches
// EntryWriter's WriteVLV.
int ReadVLV(size_t offset, uint64_t* output) const;
ProfilerSession(const ProfilerSession&);
void operator=(const ProfilerSession&);
static uint8_t GetByte(uint64_t value, size_t byte_index);
static std::map<int64_t, ProfilerSession*> sessions_;
};
} // namespace oemprofiler
} // namespace wvcdm
#endif

View File

@@ -0,0 +1,40 @@
// Copyright 2016 Google Inc. All Rights Reserved.
#ifndef WVCDM_PROFILER_STATS_H_
#define WVCDM_PROFILER_STATS_H_
#include <stdint.h>
namespace wvcdm {
namespace oemprofiler {
class Stat {
public:
Stat();
void Update(uint64_t sample);
void Reset();
uint64_t GetMin() const;
uint64_t GetMax() const;
uint64_t GetSampleSize() const;
double GetMean() const;
double GetVariance() const;
private:
uint64_t min_;
uint64_t max_;
double mean_;
uint64_t count_;
double sdev_m_;
double sdev_s_;
Stat(const Stat&);
void operator=(const Stat&);
};
} // namespace oemprofiler
} // namespace wvcdm
#endif