Home | History | Annotate | Download | only in platform
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/platform/tracing.h"
     17 
     18 #include <atomic>
     19 #include <map>
     20 #include <string>
     21 #include <vector>
     22 #include "tensorflow/core/lib/strings/str_util.h"
     23 #include "tensorflow/core/lib/strings/strcat.h"
     24 #include "tensorflow/core/platform/logging.h"
     25 
     26 namespace tensorflow {
     27 
     28 namespace port {
     29 
     30 int32 Tracing::category_id_[kEventCategoryMax];
     31 uint64 Tracing::event_mask_ = 0;
     32 std::map<string, int32>* Tracing::name_map_ = new std::map<string, int32>;
     33 
     34 // This needs to be kept in sync with the EventCategory enumeration.
     35 const char* Tracing::EventCategoryString(EventCategory category) {
     36   switch (category) {
     37     case EventCategory::kScheduleClosure:
     38       return "ScheduleClosure";
     39     case EventCategory::kRunClosure:
     40       return "RunClosure";
     41     case EventCategory::kCompute:
     42       return "Compute";
     43     case EventCategory::kEventCategoryMax:
     44       return "EventCategoryMax";
     45   }
     46   return "Unknown";
     47 }
     48 
     49 // This function allows the user to specify arbitrary subsets of the
     50 // supported Threadscape events and activities.
     51 bool Tracing::ParseEventMask(const char* flagname, const string& value) {
     52   VLOG(1) << flagname << " set to " << value;
     53   int64 new_mask = 0;
     54   std::vector<string> events =
     55       str_util::Split(value, ',', str_util::SkipEmpty());
     56   for (string name : events) {
     57     bool clear = false;
     58     int64 mask = 0;
     59     if (name[0] == '!') {
     60       // invert the sense of the flag
     61       clear = true;
     62       name = name.substr(1);
     63     }
     64     if (name == "ALL") {
     65       mask = ~0;
     66     } else {
     67       auto it = name_map_->find(name);
     68       int32 id;
     69       if (it == name_map_->end()) {
     70         id = -1;
     71       } else {
     72         id = it->second;
     73       }
     74       if (id < 0) {
     75         LOG(ERROR) << "Can't parse event mask name " << name;
     76         return false;
     77       }
     78       mask = 1 << id;
     79     }
     80     if (clear) {
     81       new_mask &= ~mask;
     82     } else {
     83       new_mask |= mask;
     84     }
     85   }
     86   // parsing was successful; set the permanent event mask
     87   event_mask_ = new_mask;
     88   return true;
     89 }
     90 
     91 /*static*/ std::atomic<Tracing::Engine*> Tracing::tracing_engine_;
     92 
     93 void Tracing::RegisterEngine(Engine* e) {
     94   tracing_engine_.store(e, std::memory_order_release);
     95 }
     96 
     97 Tracing::Engine::~Engine() {}
     98 Tracing::Engine::Annotation::~Annotation() {}
     99 Tracing::Engine::Tracer::~Tracer() {}
    100 
    101 }  // namespace port
    102 }  // namespace tensorflow
    103