summaryrefslogtreecommitdiff
path: root/indra/newview/llviewerassetstats.h
blob: 73ec5974b296928b227a890c3ff2b69bb8a59c46 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/** 
 * @file llviewerassetstats.h
 * @brief Client-side collection of asset request statistics
 *
 * $LicenseInfo:firstyear=2010&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#ifndef LL_LLVIEWERASSETSTATUS_H
#define	LL_LLVIEWERASSETSTATUS_H


#include "linden_common.h"

#include "llpointer.h"
#include "llrefcount.h"
#include "llviewerassettype.h"
#include "llviewerassetstorage.h"
#include "llsimplestat.h"
#include "llsd.h"

/**
 * @class LLViewerAssetStats
 * @brief Records performance aspects of asset access operations.
 *
 * This facility is derived from a very similar simulator-based
 * one, LLSimAssetStats.  It's function is to count asset access
 * operations and characterize response times.  Collected data
 * are binned in several dimensions:
 *
 *  - Asset types collapsed into a few aggregated categories
 *  - By simulator UUID
 *  - By transport mechanism (HTTP vs MessageSystem)
 *  - By persistence (temp vs non-temp)
 *
 * Statistics collected are fairly basic at this point:
 *
 *  - Counts of enqueue and dequeue operations
 *  - Min/Max/Mean of asset transfer operations
 *
 * This collector differs from the simulator-based on in a
 * number of ways:
 *
 *  - The front-end/back-end distinction doesn't exist in viewer
 *    code
 *  - Multiple threads must be safely accomodated in the viewer
 *
 * Access to results is by conversion to an LLSD with some standardized
 * key names.  The intent of this structure is that it be emitted as
 * standard syslog-based metrics formatting where it can be picked
 * up by interested parties.
 *
 * For convenience, a set of free functions in namespace
 * LLViewerAssetStatsFF is provided for conditional test-and-call
 * operations.
 */
class LLViewerAssetStats
{
public:
	enum EViewerAssetCategories
	{
		EVACTextureTempHTTPGet,			//< Texture GETs - temp/baked, HTTP
		EVACTextureTempUDPGet,			//< Texture GETs - temp/baked, UDP
		EVACTextureNonTempHTTPGet,		//< Texture GETs - perm, HTTP
		EVACTextureNonTempUDPGet,		//< Texture GETs - perm, UDP
		EVACWearableUDPGet,				//< Wearable GETs
		EVACSoundUDPGet,				//< Sound GETs
		EVACGestureUDPGet,				//< Gesture GETs
		EVACOtherGet,					//< Other GETs
		
		EVACCount						// Must be last
	};

	/**
	 * Type for duration and other time values in the metrics.  Selected
	 * for compatibility with the pre-existing timestamp on the texture
	 * fetcher class, LLTextureFetch.
	 */
	typedef U64 duration_t;

	/**
	 * Type for the region identifier used in stats.  Currently uses
	 * the region handle's type (a U64) rather than the regions's LLUUID
	 * as the latter isn't available immediately.
	 */
	typedef U64 region_handle_t;

	/**
	 * @brief Collected data for a single region visited by the avatar.
	 *
	 * Fairly simple, for each asset bin enumerated above a count
	 * of enqueue and dequeue operations and simple stats on response
	 * times for completed requests.
	 */
	class PerRegionStats : public LLRefCount
	{
	public:
		PerRegionStats(const region_handle_t region_handle)
			: LLRefCount(),
			  mRegionHandle(region_handle)
			{
				reset();
			}

		PerRegionStats(const PerRegionStats & src)
			: LLRefCount(),
			  mRegionHandle(src.mRegionHandle),
			  mTotalTime(src.mTotalTime),
			  mStartTimestamp(src.mStartTimestamp),
			  mFPS(src.mFPS)
			{
				for (int i = 0; i < LL_ARRAY_SIZE(mRequests); ++i)
				{
					mRequests[i] = src.mRequests[i];
				}
			}

		// Default assignment and destructor are correct.
		
		void reset();

		void merge(const PerRegionStats & src);
		
		// Apply current running time to total and reset start point.
		// Return current timestamp as a convenience.
		void accumulateTime(duration_t now);
		
	public:
		region_handle_t		mRegionHandle;
		duration_t			mTotalTime;
		duration_t			mStartTimestamp;
		LLSimpleStatMMM<>	mFPS;
		
		struct prs_group
		{
			LLSimpleStatCounter			mEnqueued;
			LLSimpleStatCounter			mDequeued;
			LLSimpleStatMMM<duration_t>	mResponse;
		}
		mRequests [EVACCount];
	};

public:
	LLViewerAssetStats();
	LLViewerAssetStats(const LLViewerAssetStats &);
	// Default destructor is correct.
	LLViewerAssetStats & operator=(const LLViewerAssetStats &);			// Not defined

	// Clear all metrics data.  This leaves the currently-active region
	// in place but with zero'd data for all metrics.  All other regions
	// are removed from the collection map.
	void reset();

	// Set hidden region argument and establish context for subsequent
	// collection calls.
	void setRegion(region_handle_t region_handle);

	// Asset GET Requests
	void recordGetEnqueued(LLViewerAssetType::EType at, bool with_http, bool is_temp);
	void recordGetDequeued(LLViewerAssetType::EType at, bool with_http, bool is_temp);
	void recordGetServiced(LLViewerAssetType::EType at, bool with_http, bool is_temp, duration_t duration);

	// Frames-Per-Second Samples
	void recordFPS(F32 fps);

	// Merge a source instance into a destination instance.  This is
	// conceptually an 'operator+=()' method:
	// - counts are added
	// - minimums are min'd
	// - maximums are max'd
	// - other scalars are ignored ('this' wins)
	//
	void merge(const LLViewerAssetStats & src);
	
	// Retrieve current metrics for all visited regions (NULL region UUID/handle excluded)
    // Returned LLSD is structured as follows:
	//
	// &stats_group = {
	//   enqueued   : int,
	//   dequeued   : int,
	//   resp_count : int,
	//   resp_min   : float,
	//   resp_max   : float,
	//   resp_mean  : float
	// }
	//
	// &mmm_group = {
	//   count : int,
	//   min   : float,
	//   max   : float,
	//   mean  : float
	// }
	//
	// {
	//   duration: int
	//   regions: {
	//     $: {			// Keys are strings of the region's handle in hex
	//       duration:                 : int,
	//		 fps:					   : &mmm_group,
	//       get_texture_temp_http     : &stats_group,
	//       get_texture_temp_udp      : &stats_group,
	//       get_texture_non_temp_http : &stats_group,
	//       get_texture_non_temp_udp  : &stats_group,
	//       get_wearable_udp          : &stats_group,
	//       get_sound_udp             : &stats_group,
	//       get_gesture_udp           : &stats_group,
	//       get_other                 : &stats_group
	//     }
	//   }
	// }
	//
	// @param	compact_output		If true, omits from conversion any mmm_block
	//								or stats_block that would contain all zero data.
	//								Useful for transmission when the receiver knows
	//								what is expected and will assume zero for missing
	//								blocks.
	LLSD asLLSD(bool compact_output);

protected:
	typedef std::map<region_handle_t, LLPointer<PerRegionStats> > PerRegionContainer;

	// Region of the currently-active region.  Always valid but may
	// be zero after construction or when explicitly set.  Unchanged
	// by a reset() call.
	region_handle_t mRegionHandle;

	// Pointer to metrics collection for currently-active region.  Always
	// valid and unchanged after reset() though contents will be changed.
	// Always points to a collection contained in mRegionStats.
	LLPointer<PerRegionStats> mCurRegionStats;

	// Metrics data for all regions during one collection cycle
	PerRegionContainer mRegionStats;

	// Time of last reset
	duration_t mResetTimestamp;
};


/**
 * Global stats collectors one for each independent thread where
 * assets and other statistics are gathered.  The globals are
 * expected to be created at startup time and then picked up by
 * their respective threads afterwards.  A set of free functions
 * are provided to access methods behind the globals while both
 * minimally disrupting visual flow and supplying a description
 * of intent.
 *
 * Expected thread assignments:
 *
 *  - Main:  main() program execution thread
 *  - Thread1:  TextureFetch worker thread
 */
extern LLViewerAssetStats * gViewerAssetStatsMain;

extern LLViewerAssetStats * gViewerAssetStatsThread1;

namespace LLViewerAssetStatsFF
{
/**
 * @brief Allocation and deallocation of globals.
 *
 * init() should be called before threads are started that will access it though
 * you'll likely get away with calling it afterwards.  cleanup() should only be
 * called after threads are shutdown to prevent races on the global pointers.
 */
void init();

void cleanup();

/**
 * We have many timers, clocks etc. in the runtime.  This is the
 * canonical timestamp for these metrics which is compatible with
 * the pre-existing timestamping in the texture fetcher.
 */
inline LLViewerAssetStats::duration_t get_timestamp()
{
	return LLTimer::getTotalTime();
}

/**
 * Region context, event and duration loggers for the Main thread.
 */
void set_region_main(LLViewerAssetStats::region_handle_t region_handle);

void record_enqueue_main(LLViewerAssetType::EType at, bool with_http, bool is_temp);

void record_dequeue_main(LLViewerAssetType::EType at, bool with_http, bool is_temp);

void record_response_main(LLViewerAssetType::EType at, bool with_http, bool is_temp,
						  LLViewerAssetStats::duration_t duration);

void record_fps_main(F32 fps);


/**
 * Region context, event and duration loggers for Thread 1.
 */
void set_region_thread1(LLViewerAssetStats::region_handle_t region_handle);

void record_enqueue_thread1(LLViewerAssetType::EType at, bool with_http, bool is_temp);

void record_dequeue_thread1(LLViewerAssetType::EType at, bool with_http, bool is_temp);

void record_response_thread1(LLViewerAssetType::EType at, bool with_http, bool is_temp,
						  LLViewerAssetStats::duration_t duration);

} // namespace LLViewerAssetStatsFF

#endif // LL_LLVIEWERASSETSTATUS_H