diff --git a/libwvdrmengine/cdm/Android.mk b/libwvdrmengine/cdm/Android.mk index ff65d58d..809c94cb 100644 --- a/libwvdrmengine/cdm/Android.mk +++ b/libwvdrmengine/cdm/Android.mk @@ -39,7 +39,9 @@ LOCAL_SRC_FILES := \ $(PROFILER_SRC_DIR)/entry_writer.cpp \ $(PROFILER_SRC_DIR)/profiled_scope.cpp \ $(PROFILER_SRC_DIR)/stats.cpp \ - $(PROFILER_SRC_DIR)/profiler_session.cpp + $(PROFILER_SRC_DIR)/profiler.cpp \ + $(PROFILER_SRC_DIR)/call_table.cpp \ + $(PROFILER_SRC_DIR)/call_history.cpp LOCAL_MODULE := libcdm LOCAL_MODULE_TAGS := optional diff --git a/libwvdrmengine/cdm/core/src/oemcrypto_adapter_dynamic.cpp b/libwvdrmengine/cdm/core/src/oemcrypto_adapter_dynamic.cpp index 7272f651..1a5dc4a7 100644 --- a/libwvdrmengine/cdm/core/src/oemcrypto_adapter_dynamic.cpp +++ b/libwvdrmengine/cdm/core/src/oemcrypto_adapter_dynamic.cpp @@ -23,7 +23,6 @@ #include "lock.h" #include "log.h" #include "profiled_scope.h" -#include "profiler_session.h" #include "properties.h" using namespace wvoec3; @@ -514,10 +513,6 @@ class Adapter { session_map_[*session] = new_session; } - if (result == OEMCrypto_SUCCESS) { - wvcdm::oemprofiler::ProfilerSession::Open(static_cast(*session)); - } - return result; } @@ -534,10 +529,6 @@ class Adapter { pair->second.fcn->CloseSession(pair->second.session); session_map_.erase(pair); - if (result == OEMCrypto_SUCCESS) { - wvcdm::oemprofiler::ProfilerSession::Close(static_cast(session)); - } - return result; } @@ -739,16 +730,6 @@ OEMCryptoResult OEMCrypto_GetMaxNumberOfSessions(SecurityLevel level, } // namespace wvcdm extern "C" OEMCryptoResult OEMCrypto_Initialize(void) { - - if (wvcdm::oemprofiler::ProfilerSession::Find( - wvcdm::oemprofiler::ProfiledScope::kGlobalSID) != NULL) { - wvcdm::oemprofiler::ProfilerSession::Close( - wvcdm::oemprofiler::ProfiledScope::kGlobalSID); - } - - wvcdm::oemprofiler::ProfilerSession::Open( - wvcdm::oemprofiler::ProfiledScope::kGlobalSID); - if (kAdapter) { kAdapter->Terminate(); delete kAdapter; @@ -758,13 +739,6 @@ extern "C" OEMCryptoResult OEMCrypto_Initialize(void) { } extern "C" OEMCryptoResult OEMCrypto_Terminate(void) { - - if (wvcdm::oemprofiler::ProfilerSession::Find( - wvcdm::oemprofiler::ProfiledScope::kGlobalSID) != NULL) { - wvcdm::oemprofiler::ProfilerSession::Close( - wvcdm::oemprofiler::ProfiledScope::kGlobalSID); - } - OEMCryptoResult result = OEMCrypto_SUCCESS; if (kAdapter) { result = kAdapter->Terminate(); @@ -788,7 +762,7 @@ extern "C" OEMCryptoResult OEMCrypto_GenerateDerivedKeys( uint32_t mac_key_context_length, const uint8_t* enc_key_context, uint32_t enc_key_context_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_GENERATE_DERIVED_KEYS); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -802,7 +776,7 @@ extern "C" OEMCryptoResult OEMCrypto_GenerateDerivedKeys( extern "C" OEMCryptoResult OEMCrypto_GenerateNonce(OEMCrypto_SESSION session, uint32_t* nonce) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_GENERATE_NONCE); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -815,7 +789,7 @@ extern "C" OEMCryptoResult OEMCrypto_GenerateSignature( OEMCrypto_SESSION session, const uint8_t* message, size_t message_length, uint8_t* signature, size_t* signature_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_GENERATE_SIGNATURE); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -832,7 +806,7 @@ extern "C" OEMCryptoResult OEMCrypto_LoadKeys( const OEMCrypto_KeyObject* key_array, const uint8_t* pst, size_t pst_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_LOAD_KEYS); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -876,7 +850,7 @@ extern "C" OEMCryptoResult OEMCrypto_RefreshKeys( const uint8_t* signature, size_t signature_length, size_t num_keys, const OEMCrypto_KeyRefreshObject* key_array) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_REFRESH_KEYS); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -890,7 +864,7 @@ extern "C" OEMCryptoResult OEMCrypto_QueryKeyControl( OEMCrypto_SESSION session, const uint8_t* key_id, size_t key_id_length, uint8_t* key_control_block, size_t* key_control_block_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_QUERY_KEY_CONTROL); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -905,7 +879,7 @@ extern "C" OEMCryptoResult OEMCrypto_SelectKey(const OEMCrypto_SESSION session, const uint8_t* key_id, size_t key_id_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_SELECT_KEY); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -921,7 +895,7 @@ extern "C" OEMCryptoResult OEMCrypto_DecryptCENC( const OEMCrypto_CENCEncryptPatternDesc* pattern, uint8_t subsample_flags) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_DECRYPT_CENC); ps.meta_data_.WriteVLV(static_cast(data_length)); @@ -1014,7 +988,7 @@ extern "C" OEMCryptoResult OEMCrypto_RewrapDeviceRSAKey( const uint8_t* enc_rsa_key_iv, uint8_t* wrapped_rsa_key, size_t* wrapped_rsa_key_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_REWRAP_DEVICE_RSA_KEY); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -1030,7 +1004,7 @@ extern "C" OEMCryptoResult OEMCrypto_LoadDeviceRSAKey( OEMCrypto_SESSION session, const uint8_t* wrapped_rsa_key, size_t wrapped_rsa_key_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_LOAD_DEVICE_RSA_KEY); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -1057,7 +1031,7 @@ extern "C" OEMCryptoResult OEMCrypto_GenerateRSASignature( uint8_t* signature, size_t* signature_length, RSA_Padding_Scheme padding_scheme) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_GENERATE_RSA_SIGNATURE); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -1079,7 +1053,7 @@ extern "C" OEMCryptoResult OEMCrypto_DeriveKeysFromSessionKey( size_t mac_key_context_length, const uint8_t* enc_key_context, size_t enc_key_context_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_DERIVE_KEYS_FROM_SESSION_KEY); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -1127,7 +1101,7 @@ extern "C" OEMCryptoResult OEMCrypto_Generic_Encrypt( OEMCrypto_SESSION session, const uint8_t* in_buffer, size_t buffer_length, const uint8_t* iv, OEMCrypto_Algorithm algorithm, uint8_t* out_buffer) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_GENERIC_ENCRYPT); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -1141,7 +1115,7 @@ extern "C" OEMCryptoResult OEMCrypto_Generic_Decrypt( OEMCrypto_SESSION session, const uint8_t* in_buffer, size_t buffer_length, const uint8_t* iv, OEMCrypto_Algorithm algorithm, uint8_t* out_buffer) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_GENERIC_DECRYPT); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -1158,7 +1132,7 @@ extern "C" OEMCryptoResult OEMCrypto_Generic_Sign(OEMCrypto_SESSION session, uint8_t* signature, size_t* signature_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_GENERIC_SIGN); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -1224,7 +1198,7 @@ extern "C" OEMCryptoResult OEMCrypto_ReportUsage(OEMCrypto_SESSION session, OEMCrypto_PST_Report* buffer, size_t* buffer_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_REPORT_USAGE); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; @@ -1243,7 +1217,7 @@ extern "C" OEMCryptoResult OEMCrypto_DeleteUsageEntry( const uint8_t* message, size_t message_length, const uint8_t* signature, size_t signature_length) { - wvcdm::oemprofiler::ProfiledScope ps(session, + wvcdm::oemprofiler::ProfiledScope ps( wvcdm::oemprofiler::OEM_FUNCTION_DELETE_USAGE_ENTRY); if (!kAdapter) return OEMCrypto_ERROR_UNKNOWN_FAILURE; diff --git a/libwvdrmengine/cdm/profiler/include/call_history.h b/libwvdrmengine/cdm/profiler/include/call_history.h new file mode 100644 index 00000000..6ffee95a --- /dev/null +++ b/libwvdrmengine/cdm/profiler/include/call_history.h @@ -0,0 +1,49 @@ +// Copyright 2016 Google Inc. All Rights Reserved. + +#ifndef WVCDM_CALL_HISTORY_H_ +#define WVCDM_CALL_HISTORY_H_ + +#include +#include + +#include "circular_buffer.h" +#include "oem_functions.h" + +namespace wvcdm { +namespace oemprofiler { + +class CallHistory { + + public: + CallHistory(); + + void Write( + OEM_FUNCTION fid, + uint64_t start_time, + uint64_t end_time, + const uint8_t* meta_data, + size_t meta_data_length); + + void Read(std::vector& output) const; + + private: + 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; +}; + +} // namespace oemprofiler +} // namespace wvcdm + +#endif diff --git a/libwvdrmengine/cdm/profiler/include/call_table.h b/libwvdrmengine/cdm/profiler/include/call_table.h new file mode 100644 index 00000000..f84c4ff9 --- /dev/null +++ b/libwvdrmengine/cdm/profiler/include/call_table.h @@ -0,0 +1,49 @@ +#ifndef WVCDM_PROFILER_CALL_TABLE_H_ +#define WVCDM_PROFILER_CALL_TABLE_H_ + +#include +#include +#include + +namespace wvcdm { +namespace oemprofiler { + +class CallTable { + public: + class Row { + public: + Row(); + void Add(uint64_t sample); + + uint64_t GetSampleSize() const; + + uint64_t GetMin() const; + uint64_t GetMax() const; + + double GetMean() const; + double GetVariance() const; + + private: + uint64_t min_; + uint64_t max_; + uint64_t sample_size_; + double mean_; + double variance_m_; + double variance_s_; + + }; + + const Row* LookUp(uint64_t row_id) const; + + void Write(uint64_t row_id, uint64_t sample); + + void Read(std::vector& output) const; + + private: + std::map map_; +}; + +} // namespace oemprofiler +} // namespace wvcdm + +#endif diff --git a/libwvdrmengine/cdm/profiler/include/profiled_scope.h b/libwvdrmengine/cdm/profiler/include/profiled_scope.h index 64fcb910..b218ca49 100644 --- a/libwvdrmengine/cdm/profiler/include/profiled_scope.h +++ b/libwvdrmengine/cdm/profiler/include/profiled_scope.h @@ -14,26 +14,16 @@ 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; + void Submit(uint64_t end_time) const; uint64_t GetNowUS() const; // disallow copy and assign diff --git a/libwvdrmengine/cdm/profiler/include/profiler.h b/libwvdrmengine/cdm/profiler/include/profiler.h new file mode 100644 index 00000000..f12c1360 --- /dev/null +++ b/libwvdrmengine/cdm/profiler/include/profiler.h @@ -0,0 +1,25 @@ +// Copyright 2016 Google Inc. All Rights Reserved. + +#ifndef WVCDM_PROFILER_H_ +#define WVCDM_PROFILER_H_ + +#include "call_table.h" +#include "call_history.h" + +namespace wvcdm { +namespace oemprofiler { + +class Profiler { + public: + static CallTable& GetTable(); + static CallHistory& GetHistory(); + + private: + static CallTable global_table_; + static CallHistory global_history_; +}; + +} // namespace oemprofiler +} // namespace wvcdm + +#endif diff --git a/libwvdrmengine/cdm/profiler/src/call_history.cpp b/libwvdrmengine/cdm/profiler/src/call_history.cpp new file mode 100644 index 00000000..534c4a7d --- /dev/null +++ b/libwvdrmengine/cdm/profiler/src/call_history.cpp @@ -0,0 +1,170 @@ +// Copyright 2016 Google Inc. All Rights Reserved. + +#include "call_history.h" + +#include + +#include "entry_writer.h" + +namespace wvcdm { +namespace oemprofiler { + +namespace { + const size_t kProfilingMemoryBudget = 64 * 1024; // 64 KB + const uint8_t kOutputVersionNumber = 0x00; +} + +CallHistory::CallHistory() : + buffer_(kProfilingMemoryBudget), + time_at_head_(0), + time_at_tail_(0) { + +} + +void CallHistory::Write( + OEM_FUNCTION fid, + uint64_t start_time, + uint64_t end_time, + const uint8_t* meta_data, + size_t meta_data_length) { + + // time_at_tail <= start_time <= end_time or else the subtraction + // will be invalid + if (start_time > end_time || start_time < time_at_tail_) { + LOGE("Skipping submission. Submission has time travelled." + "Start=%llu End=%llu Tail=%llu. Should be Tail<=Start<=End.", + start_time, end_time, time_at_tail_); + return; + } + + EntryWriter header; + header.WriteU8(fid); + header.WriteVLV(start_time - time_at_tail_); + header.WriteVLV(end_time - start_time); + + const size_t total_packet_size = header.GetSize() + meta_data_length; + + // The max size for a VLV is 8 bytes and the max size for a entry + // writer is 32 bytes. Normally the meta data will be packed using + // an entry writer so the max packet size will be 64 bytes. Since the + // packet size is encoded with a single byte, the packet must first + // be checked to ensure it is not too large for the cast. + if (total_packet_size <= 255 && RequestSpace(total_packet_size + 1)) { + buffer_.AddU8(static_cast(total_packet_size)); + buffer_.AddU8s(header.GetData(), header.GetSize()); + buffer_.AddU8s(meta_data, meta_data_length); + + time_at_tail_ = end_time; + } +} + +void CallHistory::Read(std::vector& output) const { + output.push_back(kOutputVersionNumber); + + // write the starting time + EntryWriter startingTimeWriter; + if (-1 == startingTimeWriter.WriteVLV(time_at_head_)) { + output.clear(); + return; + } + + for (size_t i = 0; i < startingTimeWriter.GetSize(); i++) { + output.push_back(startingTimeWriter.GetData()[i]); + } + + // write the whole circular buffer into the output buffer + const size_t num_bytes = buffer_.GetUsedSpace(); + for (size_t i = 0; i < num_bytes; i++) { + uint8_t b; + if (buffer_.PeekU8(i, &b)) { + output.push_back(b); + } + } +} + +bool CallHistory::RequestSpace(uint8_t num_bytes) { + // check if it is possible to make enough room + const size_t buffer_size = buffer_.GetFreeSpace() + + buffer_.GetUsedSpace(); + + if (num_bytes > buffer_size) { + LOGE("Requesting more space than possible (requested = %u, max = %zu)", + num_bytes, buffer_size); + return false; + } + + // drop entries until we have enough space + while (num_bytes > buffer_.GetFreeSpace() && DropLastEntry()); + + return num_bytes <= buffer_.GetFreeSpace(); +} + +bool CallHistory::ReadNextEntryRealEndTime(uint64_t* output) { + if (output == NULL) { + LOGE("Cannout output to null pointer"); + return false; + } + + size_t initial_time_start_index = 2; + + uint64_t initial_time; + const int initial_time_length = + ReadVLV(initial_time_start_index, &initial_time); + if (initial_time_length == -1) { + LOGE("Failed to read the start time for head entry"); + return false; + } + + uint64_t delta_time; + const int delta_time_length = ReadVLV( + initial_time_start_index + initial_time_length, &delta_time); + + if (delta_time_length == -1) { + LOGE("Failed to read the delta time for head entry"); + return false; + } + + *output = time_at_head_ + initial_time + delta_time; + return true; +} + +bool CallHistory::DropLastEntry() { + uint8_t entry_size; + uint64_t end_time; + + if (buffer_.PeekU8(0, &entry_size) && ReadNextEntryRealEndTime(&end_time)) { + // + 1 because the entry size byte needs to be removed too + if (buffer_.Remove(entry_size + 1)) { + time_at_head_ = end_time; + return true; + } + } + + return false; +} + +int CallHistory::ReadVLV(size_t offset, uint64_t* output) const { + uint8_t first_byte; + if (buffer_.PeekU8(offset, &first_byte)) { + const size_t num_bytes = (first_byte >> 5) + 1; + + uint64_t value = first_byte & 0x1F; + for (size_t i = 1; i < num_bytes; i++) { + uint8_t next_byte; + if (buffer_.PeekU8(offset + i, &next_byte)) { + value = value << 8 | next_byte; + } else { + return -1; + } + } + + *output = value; + return num_bytes; + } + + return -1; +} + +} // namespace wvcdm +} // namespace oemprofiler + diff --git a/libwvdrmengine/cdm/profiler/src/call_table.cpp b/libwvdrmengine/cdm/profiler/src/call_table.cpp new file mode 100644 index 00000000..fe1b2306 --- /dev/null +++ b/libwvdrmengine/cdm/profiler/src/call_table.cpp @@ -0,0 +1,103 @@ +#include "call_table.h" + +#include + +#include "entry_writer.h" + +namespace wvcdm { +namespace oemprofiler { + +namespace { + const uint8_t kOutputVersionNumber = 0x00; +} + +CallTable::Row::Row() : + min_(std::numeric_limits::max()), + max_(0), + sample_size_(0), + mean_(0), + variance_m_(0), + variance_s_(0) { + +} + +void CallTable::Row::Add(uint64_t value) { + min_ = std::min(min_, value); + max_ = std::max(max_, value); + + mean_ = ((mean_ * sample_size_) + value) / (sample_size_ + 1.0); + sample_size_ += 1; + + // Welford's method for standard deviation and variance + const double old_m = variance_m_; + const double old_s = variance_s_; + variance_m_ = old_m + (value - old_m) / sample_size_; + variance_s_ = old_s + (value - variance_m_) * (value -old_m); +} + +uint64_t CallTable::Row::GetSampleSize() const { + return sample_size_; +} + +uint64_t CallTable::Row::GetMin() const { + return min_; +} + +uint64_t CallTable::Row::GetMax() const { + return max_; +} + +double CallTable::Row::GetMean() const { + return mean_; +} + +double CallTable::Row::GetVariance() const { + return sample_size_ > 1 ? variance_s_ / (sample_size_ - 1) : 0; +} + +const CallTable::Row* CallTable::LookUp(uint64_t row_id) const { + return map_.count(row_id) == 0 ? NULL : &map_.find(row_id)->second; +} + +void CallTable::Write(uint64_t row_id, uint64_t sample) { + if (map_.count(row_id) == 0) { + map_.insert(std::pair(row_id, Row())); + } + + map_[row_id].Add(sample); +} + +void CallTable::Read(std::vector& output) const { + output.push_back(kOutputVersionNumber); + + for (std::map::const_iterator it = map_.begin(); + it != map_.end(); ++it) { + + const uint64_t values_to_write[] = { + it->first, + it->second.GetSampleSize(), + it->second.GetMin(), + it->second.GetMax(), + static_cast(it->second.GetMean()), + // get the decimal places to the one-hundredths + static_cast(it->second.GetMean() * 100) % 100, + static_cast(it->second.GetVariance()), + // get the decimal places to the one-hundredths + static_cast(it->second.GetVariance() * 100) % 100 + }; + + const size_t kValuesToWriteSize = 8; + + for (size_t i = 0; i < kValuesToWriteSize; i++) { + EntryWriter writer; + writer.WriteVLV(values_to_write[i]); + + for (size_t w_i = 0; w_i < writer.GetSize(); w_i++) { + output.push_back(writer.GetData()[w_i]); + } + } + } +} + +} // namespace oemprofiler +} // namespace wvcdm diff --git a/libwvdrmengine/cdm/profiler/src/profiled_scope.cpp b/libwvdrmengine/cdm/profiler/src/profiled_scope.cpp index 2cf687a7..5e7fefe2 100644 --- a/libwvdrmengine/cdm/profiler/src/profiled_scope.cpp +++ b/libwvdrmengine/cdm/profiler/src/profiled_scope.cpp @@ -4,53 +4,31 @@ #include -#include "profiler_session.h" +#include "profiler.h" namespace wvcdm { namespace oemprofiler { ProfiledScope::ProfiledScope(OEM_FUNCTION fid) : meta_data_(), - sid_(kGlobalSID), - fid_(fid), - start_time_(GetNowUS()) { - -} - -// Only allow a user provided sid to be a positive integer -// to prevent a user provided sid from conflicting with the -// global sid -ProfiledScope::ProfiledScope(uint32_t sid, OEM_FUNCTION fid) : - meta_data_(), - sid_(static_cast(sid)), fid_(fid), start_time_(GetNowUS()) { } ProfiledScope::~ProfiledScope() { - const uint64_t end_time = GetNowUS(); - - if (sid_ != kGlobalSID) { - Submit(sid_, end_time); - } - - // Always save a copy to the global session so that all other sessions - // are subsets of the global session - Submit(kGlobalSID, end_time); + Submit(GetNowUS()); } -void ProfiledScope::Submit(int64_t sid, uint64_t end_time) const { - ProfilerSession* const session = ProfilerSession::Find(sid); +void ProfiledScope::Submit(uint64_t end_time) const { + Profiler::GetTable().Write(fid_, end_time - start_time_); - if (session != NULL) { - session->Submit( + Profiler::GetHistory().Write( fid_, start_time_, end_time, meta_data_.GetData(), meta_data_.GetSize()); - } } uint64_t ProfiledScope::GetNowUS() const { diff --git a/libwvdrmengine/cdm/profiler/src/profiler.cpp b/libwvdrmengine/cdm/profiler/src/profiler.cpp new file mode 100644 index 00000000..45f0a592 --- /dev/null +++ b/libwvdrmengine/cdm/profiler/src/profiler.cpp @@ -0,0 +1,21 @@ +// Copyright 2016 Google Inc. All Rights Reserved. + +#include "profiler.h" + +namespace wvcdm { +namespace oemprofiler { + +CallTable Profiler::global_table_; +CallHistory Profiler::global_history_; + +CallTable& Profiler::GetTable() { + return global_table_; +} + +CallHistory& Profiler::GetHistory() { + return global_history_; +} + +} // namespace wvcdm +} // namespace oemprofiler + diff --git a/libwvdrmengine/mediadrm/include/WVDrmPlugin.h b/libwvdrmengine/mediadrm/include/WVDrmPlugin.h index 4713e8f9..ed3b398d 100644 --- a/libwvdrmengine/mediadrm/include/WVDrmPlugin.h +++ b/libwvdrmengine/mediadrm/include/WVDrmPlugin.h @@ -13,7 +13,6 @@ #include "media/stagefright/foundation/ABase.h" #include "media/stagefright/foundation/AString.h" #include "OEMCryptoCENC.h" -#include "profiler_session.h" #include "utils/Errors.h" #include "utils/KeyedVector.h" #include "utils/List.h" @@ -275,11 +274,6 @@ class WVDrmPlugin : public android::DrmPlugin, status_t mapAndNotifyOfOEMCryptoResult(const Vector& sessionId, OEMCryptoResult res); - bool tryGettingSessionFromPropertyName( - const String8& name, - const String8& tag, - wvcdm::oemprofiler::ProfilerSession** out) const; - bool tryGettingOEMProfilingHistory( const String8& name, Vector& value) const; diff --git a/libwvdrmengine/mediadrm/src/WVDrmPlugin.cpp b/libwvdrmengine/mediadrm/src/WVDrmPlugin.cpp index 54564c36..0722826f 100644 --- a/libwvdrmengine/mediadrm/src/WVDrmPlugin.cpp +++ b/libwvdrmengine/mediadrm/src/WVDrmPlugin.cpp @@ -17,7 +17,7 @@ #include "mapErrors-inl.h" #include "media/stagefright/MediaErrors.h" -#include "profiled_scope.h" +#include "profiler.h" #include "utils/Errors.h" #include "wv_cdm_constants.h" @@ -30,8 +30,8 @@ namespace { // profiler proterties constants - static const android::String8 kProfilerHistoryTag("oemProfilerHistory-"); - static const android::String8 kProfilerStatsTag("oemProfilerStats-"); + static const android::String8 kProfilerHistoryTag("oemProfilerHistory"); + static const android::String8 kProfilerStatsTag("oemProfilerStats"); } namespace wvdrm { @@ -498,84 +498,33 @@ status_t WVDrmPlugin::getPropertyString(const String8& name, return android::OK; } -bool WVDrmPlugin::tryGettingSessionFromPropertyName( - const String8& name, - const String8& tag, - oemprofiler::ProfilerSession** out) const { - - if (name.find(tag) != 0) { - return false; - } - - oemprofiler::ProfilerSession* session = NULL; - - // if the name starts with the tag and is the same length - // as the tag, then it must just be the tag - if (name.length() == tag.length()) { - // if it is just the tag, then return the global session - session = oemprofiler::ProfilerSession::Find( - oemprofiler::ProfiledScope::kGlobalSID); - } else { - // make a string that is only the part that comes after the tag. - // If it is a valid property, this should be a cdm session id - const CdmSessionId cdm_session_id(name.string() + tag.length()); - - if (mCryptoSessions.count(cdm_session_id) != 1) { - return false; - } - - const OEMCrypto_SESSION sid = - mCryptoSessions.at(cdm_session_id).oecSessionId(); - - session = oemprofiler::ProfilerSession::Find(static_cast(sid)); - } - - // if the session is not open, then treat the property as not existing - if (session == nullptr) { - return false; - } - - if (out != nullptr) { - *out = session; - } - - return true; -} - bool WVDrmPlugin::tryGettingOEMProfilingHistory(const String8& name, Vector& value) const { - oemprofiler::ProfilerSession* session = nullptr; - - if (!tryGettingSessionFromPropertyName( - name, kProfilerHistoryTag, &session)) { - return false; - } - - // read the data out of the session + if (name == kProfilerHistoryTag) { std::vector tempValue; - session->ReadHistory(tempValue); + oemprofiler::Profiler::GetHistory().Read(tempValue); value.appendArray(tempValue.data(), tempValue.size()); return true; + } + + return false; } bool WVDrmPlugin::tryGettingOEMProfilingStats( const String8& name, Vector& value) const { - oemprofiler::ProfilerSession* session = nullptr; + if (name == kProfilerStatsTag) { + std::vector tempValue; + oemprofiler::Profiler::GetTable().Read(tempValue); + value.appendArray(tempValue.data(), tempValue.size()); - if (!tryGettingSessionFromPropertyName( - name, kProfilerStatsTag, &session)) { - return false; + return true; } - std::vector tempValue; - session->ReadAllStats(tempValue); - value.appendArray(tempValue.data(), tempValue.size()); - - return true; + return false; } status_t WVDrmPlugin::getPropertyByteArray(const String8& name,