Welcome to mirror list, hosted at ThFree Co, Russian Federation.

util_profiling.cpp « util « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5343f076e223f19d1263afe4666b29bc48462232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright 2011-2018 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "util/util_profiling.h"
#include "util/util_algorithm.h"
#include "util/util_foreach.h"
#include "util/util_set.h"

CCL_NAMESPACE_BEGIN

Profiler::Profiler() : do_stop_worker(true), worker(NULL)
{
}

Profiler::~Profiler()
{
  assert(worker == NULL);
}

void Profiler::run()
{
  uint64_t updates = 0;
  auto start_time = std::chrono::system_clock::now();
  while (!do_stop_worker) {
    thread_scoped_lock lock(mutex);
    foreach (ProfilingState *state, states) {
      uint32_t cur_event = state->event;
      int32_t cur_shader = state->shader;
      int32_t cur_object = state->object;

      /* The state reads/writes should be atomic, but just to be sure
       * check the values for validity anyways. */
      if (cur_event < PROFILING_NUM_EVENTS) {
        event_samples[cur_event]++;
      }

      if (cur_shader >= 0 && cur_shader < shader_samples.size()) {
        shader_samples[cur_shader]++;
      }

      if (cur_object >= 0 && cur_object < object_samples.size()) {
        object_samples[cur_object]++;
      }
    }
    lock.unlock();

    /* Relative waits always overshoot a bit, so just waiting 1ms every
     * time would cause the sampling to drift over time.
     * By keeping track of the absolute time, the wait times correct themselves -
     * if one wait overshoots a lot, the next one will be shorter to compensate. */
    updates++;
    std::this_thread::sleep_until(start_time + updates * std::chrono::milliseconds(1));
  }
}

void Profiler::reset(int num_shaders, int num_objects)
{
  bool running = (worker != NULL);
  if (running) {
    stop();
  }

  /* Resize and clear the accumulation vectors. */
  shader_hits.assign(num_shaders, 0);
  object_hits.assign(num_objects, 0);

  event_samples.assign(PROFILING_NUM_EVENTS, 0);
  shader_samples.assign(num_shaders, 0);
  object_samples.assign(num_objects, 0);

  if (running) {
    start();
  }
}

void Profiler::start()
{
  assert(worker == NULL);
  do_stop_worker = false;
  worker = new thread(function_bind(&Profiler::run, this));
}

void Profiler::stop()
{
  if (worker != NULL) {
    do_stop_worker = true;

    worker->join();
    delete worker;
    worker = NULL;
  }
}

void Profiler::add_state(ProfilingState *state)
{
  thread_scoped_lock lock(mutex);

  /* Add the ProfilingState from the list of sampled states. */
  assert(std::find(states.begin(), states.end(), state) == states.end());
  states.push_back(state);

  /* Resize thread-local hit counters. */
  state->shader_hits.assign(shader_hits.size(), 0);
  state->object_hits.assign(object_hits.size(), 0);

  /* Initialize the state. */
  state->event = PROFILING_UNKNOWN;
  state->shader = -1;
  state->object = -1;
  state->active = true;
}

void Profiler::remove_state(ProfilingState *state)
{
  thread_scoped_lock lock(mutex);

  /* Remove the ProfilingState from the list of sampled states. */
  states.erase(std::remove(states.begin(), states.end(), state), states.end());
  state->active = false;

  /* Merge thread-local hit counters. */
  assert(shader_hits.size() == state->shader_hits.size());
  for (int i = 0; i < shader_hits.size(); i++) {
    shader_hits[i] += state->shader_hits[i];
  }

  assert(object_hits.size() == state->object_hits.size());
  for (int i = 0; i < object_hits.size(); i++) {
    object_hits[i] += state->object_hits[i];
  }
}

uint64_t Profiler::get_event(ProfilingEvent event)
{
  assert(worker == NULL);
  return event_samples[event];
}

bool Profiler::get_shader(int shader, uint64_t &samples, uint64_t &hits)
{
  assert(worker == NULL);
  if (shader_samples[shader] == 0) {
    return false;
  }
  samples = shader_samples[shader];
  hits = shader_hits[shader];
  return true;
}

bool Profiler::get_object(int object, uint64_t &samples, uint64_t &hits)
{
  assert(worker == NULL);
  if (object_samples[object] == 0) {
    return false;
  }
  samples = object_samples[object];
  hits = object_hits[object];
  return true;
}

CCL_NAMESPACE_END