summaryrefslogtreecommitdiff
path: root/indra/newview/llviewerstatsrecorder.cpp
blob: 2e4002479d8968cdb89da11ec5e845d91104a164 (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
/**
 * @file llviewerstatsrecorder.cpp
 * @brief record info about viewer events to a metrics log file
 *
 * $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$
 */

#include "llviewerprecompiledheaders.h"
#include "llviewerstatsrecorder.h"


#include "llcontrol.h"
#include "llfile.h"
#include "llviewerregion.h"
#include "llviewerobject.h"
#include "llworld.h"

extern LLControlGroup  gSavedSettings;

LLViewerStatsRecorder* LLViewerStatsRecorder::sInstance = NULL;
LLViewerStatsRecorder::LLViewerStatsRecorder() :
    mStatsFile(NULL),
    mTimer(),
    mFileOpenTime(0.0),
    mLastSnapshotTime(0.0),
    mEnableStatsRecording(false),
    mEnableStatsLogging(false),
    mInterval(0.2),
    mMaxDuration(300.f),
    mSkipSaveIfZeros(false)
{
    if (NULL != sInstance)
    {
        LL_ERRS() << "Attempted to create multiple instances of LLViewerStatsRecorder!" << LL_ENDL;
    }
    sInstance = this;
    clearStats();
}

LLViewerStatsRecorder::~LLViewerStatsRecorder()
{
    if (mStatsFile)
    {
        writeToLog(0.f);  // Save last data
        closeStatsFile();
    }
}

void LLViewerStatsRecorder::clearStats()
{
    mObjectCacheHitCount = 0;
    mObjectCacheMissFullCount = 0;
    mObjectCacheMissCrcCount = 0;
    mObjectFullUpdates = 0;
    mObjectTerseUpdates = 0;
    mObjectCacheMissRequests = 0;
    mObjectCacheUpdateDupes = 0;
    mObjectCacheUpdateChanges = 0;
    mObjectCacheUpdateAdds = 0;
    mObjectCacheUpdateReplacements = 0;
    mObjectUpdateFailures = 0;
    mTextureFetchCount = 0;
    mMeshLoadedCount = 0;
    mObjectKills = 0;
}


void LLViewerStatsRecorder::enableObjectStatsRecording(bool enable, bool logging /* false */)
{
    mEnableStatsRecording = enable;

    // if logging is stopping, close the file
    if (mStatsFile && !logging)
    {
        writeToLog(0.f);  // Save last data
        closeStatsFile();
    }
    mEnableStatsLogging = logging;
}



void LLViewerStatsRecorder::recordCacheMissEvent(U8 cache_miss_type)
{
    if (LLViewerRegion::CACHE_MISS_TYPE_TOTAL == cache_miss_type)
    {
        mObjectCacheMissFullCount++;
    }
    else
    {
        mObjectCacheMissCrcCount++;
    }
}


void LLViewerStatsRecorder::recordObjectUpdateEvent(const EObjectUpdateType update_type)
{
    switch (update_type)
    {
    case OUT_FULL:
    case OUT_FULL_COMPRESSED:
            mObjectFullUpdates++;
        break;
    case OUT_TERSE_IMPROVED:
        mObjectTerseUpdates++;
        break;
    default:
        LL_WARNS() << "Unknown update_type" << LL_ENDL;
        break;
    };
}

void LLViewerStatsRecorder::recordCacheFullUpdate(LLViewerRegion::eCacheUpdateResult update_result)
{
    switch (update_result)
    {
        case LLViewerRegion::CACHE_UPDATE_DUPE:
            mObjectCacheUpdateDupes++;
            break;
        case LLViewerRegion::CACHE_UPDATE_CHANGED:
            mObjectCacheUpdateChanges++;
            break;
        case LLViewerRegion::CACHE_UPDATE_ADDED:
            mObjectCacheUpdateAdds++;
            break;
        case LLViewerRegion::CACHE_UPDATE_REPLACED:
            mObjectCacheUpdateReplacements++;
            break;
        default:
            LL_WARNS() << "Unknown update_result type " << (S32) update_result << LL_ENDL;
            break;
    };
}

void LLViewerStatsRecorder::writeToLog( F32 interval )
{
    if (!mEnableStatsLogging || !mEnableStatsRecording)
    {
        return;
    }

    size_t data_size = 0;
    F64 delta_time = LLFrameTimer::getTotalSeconds() - mLastSnapshotTime;
    if (delta_time < interval)
        return;

    if (mSkipSaveIfZeros)
    {
        S32 total_events = mObjectCacheHitCount + mObjectCacheMissCrcCount + mObjectCacheMissFullCount + mObjectFullUpdates +
                            mObjectTerseUpdates + mObjectCacheMissRequests + mObjectCacheUpdateDupes +
                            mObjectCacheUpdateChanges + mObjectCacheUpdateAdds + mObjectCacheUpdateReplacements + mObjectUpdateFailures;
        if (total_events == 0)
        {
            LL_DEBUGS("ILXZeroData") << "ILX: not saving zero data" << LL_ENDL;
            return;
        }
    }

    mLastSnapshotTime = LLFrameTimer::getTotalSeconds();
    LL_DEBUGS("ILX") << "ILX: "
        << mObjectCacheHitCount << " hits, "
        << mObjectCacheMissFullCount << " full misses, "
        << mObjectCacheMissCrcCount << " crc misses, "
        << mObjectFullUpdates << " full updates, "
        << mObjectTerseUpdates << " terse updates, "
        << mObjectCacheMissRequests << " cache miss requests, "
        << mObjectCacheUpdateDupes << " cache update dupes, "
        << mObjectCacheUpdateChanges << " cache update changes, "
        << mObjectCacheUpdateAdds << " cache update adds, "
        << mObjectCacheUpdateReplacements << " cache update replacements,"
        << mObjectUpdateFailures << " update failures,"
        << mTextureFetchCount << " texture fetches, "
        << mMeshLoadedCount << " mesh loads, "
        << mObjectKills << " object kills"
        << LL_ENDL;

    if (mStatsFile == NULL)
    {
        // Refresh settings
        mInterval        = gSavedSettings.getF32("StatsReportFileInterval");
        mSkipSaveIfZeros = gSavedSettings.getBOOL("StatsReportSkipZeroDataSaves");
        mMaxDuration     = gSavedSettings.getF32("StatsReportMaxDuration");

        // Open the data file
        makeStatsFileName();
        mStatsFile = LLFile::fopen(mStatsFileName, "wb");

        if (mStatsFile)
        {
            LL_INFOS("ILX") << "ILX: Writing update information to " << mStatsFileName << LL_ENDL;

            mFileOpenTime = LLFrameTimer::getTotalSeconds();

            // Write column headers
            std::ostringstream col_headers;
            col_headers << "Time (sec),"
                    << "Regions,"
                    << "Active Cached Objects,"
                    << "Cache Hits,"
                    << "Cache Full Misses,"
                    << "Cache Crc Misses,"
                    << "Full Updates,"
                    << "Terse Updates,"
                    << "Cache Miss Requests,"   // Normally results in a Full Update from simulator
                    << "Cache Update Dupes,"
                    << "Cache Update Changes,"
                    << "Cache Update Adds,"
                    << "Cache Update Replacements,"
                    << "Update Failures,"
                    << "Texture Count,"
                    << "Mesh Load Count,"
                    << "Object Kills"
                    << "\n";

            data_size = col_headers.str().size();
            if (fwrite(col_headers.str().c_str(), 1, data_size, mStatsFile ) != data_size)
            {
                LL_WARNS() << "failed to write full headers to " << mStatsFileName << LL_ENDL;
                // Close the file and turn off stats logging
                closeStatsFile();
                return;
            }
        }
        else
        {   // Failed to open file
            LL_WARNS() << "Couldn't open " << mStatsFileName << " for logging, turning off stats recording." << LL_ENDL;
            mEnableStatsLogging = false;
            return;
        }
    }

    std::ostringstream stats_data;

    stats_data << getTimeSinceStart()
        << "," << LLWorld::getInstance()->getRegionList().size()
        << "," << LLWorld::getInstance()->getNumOfActiveCachedObjects()
        << "," << mObjectCacheHitCount
        << "," << mObjectCacheMissFullCount
        << "," << mObjectCacheMissCrcCount
        << "," << mObjectFullUpdates
        << "," << mObjectTerseUpdates
        << "," << mObjectCacheMissRequests
        << "," << mObjectCacheUpdateDupes
        << "," << mObjectCacheUpdateChanges
        << "," << mObjectCacheUpdateAdds
        << "," << mObjectCacheUpdateReplacements
        << "," << mObjectUpdateFailures
        << "," << mTextureFetchCount
        << "," << mMeshLoadedCount
        << "," << mObjectKills
        << "\n";

    data_size = stats_data.str().size();
    if ( data_size != fwrite(stats_data.str().c_str(), 1, data_size, mStatsFile ))
    {
        LL_WARNS() << "Unable to write complete column data to " << mStatsFileName << LL_ENDL;
        closeStatsFile();
    }

    clearStats();

    if (getTimeSinceStart() >= mMaxDuration)
    {  // If file recording has been running for too long, stop it.
        closeStatsFile();
    }
}

void LLViewerStatsRecorder::closeStatsFile()
{
    if (mStatsFile)
    {
        LL_INFOS("ILX") << "ILX: Stopped writing update information to " << mStatsFileName << " after " << getTimeSinceStart()
                        << " seconds." << LL_ENDL;
        LLFile::close(mStatsFile);
        mStatsFile = NULL;
    }
    mEnableStatsLogging = false;
}

void LLViewerStatsRecorder::makeStatsFileName()
{
    // Create filename - tbd: use pid?
#if LL_WINDOWS
    std::string stats_file_name("SLViewerStats-");
#else
    std::string stats_file_name("slviewerstats-");
#endif

    F64         now      = LLFrameTimer::getTotalSeconds();
    std::string date_str = LLDate(now).asString();
    std::replace(date_str.begin(), date_str.end(), ':', '-');  // Make it valid for a filename
    stats_file_name.append(date_str);
    stats_file_name.append(".csv");
    mStatsFileName = gDirUtilp->getExpandedFilename(LL_PATH_LOGS, stats_file_name);
}

F32 LLViewerStatsRecorder::getTimeSinceStart()
{
    return (F32) (LLFrameTimer::getTotalSeconds() - mFileOpenTime);
}