blob: 1703efd114792e2a3a4a25ab6763fd15671b659e [file] [log] [blame]
Pierre Ossman8738e8a2015-02-11 13:49:04 +01001/* Copyright 2015 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC77be9292015-02-21 11:44:26 -06002 * Copyright (C) 2015 D. R. Commander. All Rights Reserved.
DRCb4c4a382015-02-21 11:28:37 -06003 *
Pierre Ossman8738e8a2015-02-11 13:49:04 +01004 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
DRCb4c4a382015-02-21 11:28:37 -06008 *
Pierre Ossman8738e8a2015-02-11 13:49:04 +01009 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
DRCb4c4a382015-02-21 11:28:37 -060013 *
Pierre Ossman8738e8a2015-02-11 13:49:04 +010014 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20/*
21 * This program reads files produced by TightVNC's/TurboVNC's
22 * fbs-dump, which in turn takes files from rfbproxy. It is
23 * basically a dump of the RFB protocol from the server side after
24 * the ServerInit message. Mostly this consists of FramebufferUpdate
25 * message using the HexTile encoding. Screen size and pixel format
26 * are not encoded in the file and must be specified by the user.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <math.h>
Pierre Ossman98d7af92015-11-16 09:37:46 +010032#include <sys/time.h>
Pierre Ossman8738e8a2015-02-11 13:49:04 +010033
34#include <rdr/Exception.h>
35#include <rdr/OutStream.h>
36#include <rdr/FileInStream.h>
37
38#include <rfb/PixelFormat.h>
39
40#include <rfb/CConnection.h>
41#include <rfb/CMsgReader.h>
Pierre Ossman8738e8a2015-02-11 13:49:04 +010042#include <rfb/UpdateTracker.h>
43
44#include <rfb/EncodeManager.h>
45#include <rfb/SConnection.h>
46#include <rfb/SMsgWriter.h>
47
48#include "util.h"
49
50static rfb::IntParameter width("width", "Frame buffer width", 0);
51static rfb::IntParameter height("height", "Frame buffer height", 0);
DRC4631a762015-02-21 11:57:27 -060052static rfb::IntParameter count("count", "Number of benchmark iterations", 9);
Pierre Ossman8738e8a2015-02-11 13:49:04 +010053
54static rfb::StringParameter format("format", "Pixel format (e.g. bgr888)", "");
55
DRC2a172c92015-02-25 14:18:07 -060056static rfb::BoolParameter translate("translate",
57 "Translate 8-bit and 16-bit datasets into 24-bit",
58 true);
59
Pierre Ossman8738e8a2015-02-11 13:49:04 +010060// The frame buffer (and output) is always this format
61static const rfb::PixelFormat fbPF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
62
63// Encodings to use
64static const rdr::S32 encodings[] = {
65 rfb::encodingTight, rfb::encodingCopyRect, rfb::encodingRRE,
66 rfb::encodingHextile, rfb::encodingZRLE, rfb::pseudoEncodingLastRect,
DRC562eb712015-02-21 12:01:47 -060067 rfb::pseudoEncodingQualityLevel0 + 8,
68 rfb::pseudoEncodingCompressLevel0 + 2};
Pierre Ossman8738e8a2015-02-11 13:49:04 +010069
70class DummyOutStream : public rdr::OutStream {
71public:
72 DummyOutStream();
73
74 virtual int length();
75 virtual void flush();
76
77private:
78 virtual int overrun(int itemSize, int nItems);
79
80 int offset;
81 rdr::U8 buf[131072];
82};
83
84class CConn : public rfb::CConnection {
85public:
86 CConn(const char *filename);
87 ~CConn();
88
DRC77be9292015-02-21 11:44:26 -060089 void getStats(double& ratio, unsigned long long& bytes,
90 unsigned long long& rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +010091
92 virtual void setDesktopSize(int w, int h);
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010093 virtual void setCursor(int, int, const rfb::Point&, const rdr::U8*);
Pierre Ossman8738e8a2015-02-11 13:49:04 +010094 virtual void framebufferUpdateStart();
95 virtual void framebufferUpdateEnd();
96 virtual void dataRect(const rfb::Rect&, int);
97 virtual void setColourMapEntries(int, int, rdr::U16*);
98 virtual void bell();
99 virtual void serverCutText(const char*, rdr::U32);
100
101public:
102 double decodeTime;
103 double encodeTime;
104
105protected:
106 rdr::FileInStream *in;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100107 rfb::SimpleUpdateTracker updates;
108 class SConn *sc;
109};
110
111class Manager : public rfb::EncodeManager {
112public:
113 Manager(class rfb::SConnection *conn);
114
DRC77be9292015-02-21 11:44:26 -0600115 void getStats(double&, unsigned long long&, unsigned long long&);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100116};
117
118class SConn : public rfb::SConnection {
119public:
120 SConn();
121 ~SConn();
122
123 void writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb);
124
DRC77be9292015-02-21 11:44:26 -0600125 void getStats(double&, unsigned long long&, unsigned long long&);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100126
127 virtual void setAccessRights(AccessRights ar);
128
129 virtual void setDesktopSize(int fb_width, int fb_height,
130 const rfb::ScreenSet& layout);
131
132protected:
133 DummyOutStream *out;
134 Manager *manager;
135};
136
137DummyOutStream::DummyOutStream()
138{
139 offset = 0;
140 ptr = buf;
141 end = buf + sizeof(buf);
142}
143
144int DummyOutStream::length()
145{
146 flush();
147 return offset;
148}
149
150void DummyOutStream::flush()
151{
152 offset += ptr - buf;
153 ptr = buf;
154}
155
156int DummyOutStream::overrun(int itemSize, int nItems)
157{
158 flush();
Pierre Ossmanfc331e62015-03-03 16:42:45 +0100159 if (itemSize * nItems > end - ptr)
160 nItems = (end - ptr) / itemSize;
161 return nItems;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100162}
163
164CConn::CConn(const char *filename)
165{
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100166 decodeTime = 0.0;
167 encodeTime = 0.0;
168
169 in = new rdr::FileInStream(filename);
170 setStreams(in, NULL);
171
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100172 // Need to skip the initial handshake and ServerInit
173 setState(RFBSTATE_NORMAL);
174 // That also means that the reader and writer weren't setup
175 setReader(new rfb::CMsgReader(this, in));
176 // Nor the frame buffer size and format
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100177 rfb::PixelFormat pf;
178 pf.parse(format);
179 setPixelFormat(pf);
Pierre Ossman98d7af92015-11-16 09:37:46 +0100180 setDesktopSize(width, height);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100181
182 sc = new SConn();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100183 sc->cp.setPF((bool)translate ? fbPF : pf);
DRCb4c4a382015-02-21 11:28:37 -0600184 sc->setEncodings(sizeof(encodings) / sizeof(*encodings), encodings);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100185}
186
187CConn::~CConn()
188{
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100189 delete sc;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100190 delete in;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100191}
192
DRC77be9292015-02-21 11:44:26 -0600193void CConn::getStats(double& ratio, unsigned long long& bytes,
194 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100195{
DRC77be9292015-02-21 11:44:26 -0600196 sc->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100197}
198
199void CConn::setDesktopSize(int w, int h)
200{
Pierre Ossman98d7af92015-11-16 09:37:46 +0100201 rfb::ModifiablePixelBuffer *pb;
202
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100203 CConnection::setDesktopSize(w, h);
204
Pierre Ossmanb14a6bc2018-06-18 15:44:26 +0200205 pb = new rfb::ManagedPixelBuffer((bool)translate ? fbPF : server.pf(),
206 server.width(), server.height());
Pierre Ossman98d7af92015-11-16 09:37:46 +0100207 setFramebuffer(pb);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100208}
209
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100210void CConn::setCursor(int, int, const rfb::Point&, const rdr::U8*)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100211{
212}
213
214void CConn::framebufferUpdateStart()
215{
Pierre Ossman3da238d2015-11-12 12:20:05 +0100216 CConnection::framebufferUpdateStart();
217
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100218 updates.clear();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100219 startCpuCounter();
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100220}
221
222void CConn::framebufferUpdateEnd()
223{
224 rfb::UpdateInfo ui;
Pierre Ossman9f273e92015-11-09 16:34:54 +0100225 rfb::PixelBuffer* pb = getFramebuffer();
226 rfb::Region clip(pb->getRect());
227
Pierre Ossman3da238d2015-11-12 12:20:05 +0100228 CConnection::framebufferUpdateEnd();
229
Pierre Ossman9f273e92015-11-09 16:34:54 +0100230 endCpuCounter();
231
232 decodeTime += getCpuCounter();
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100233
234 updates.getUpdateInfo(&ui, clip);
235
236 startCpuCounter();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100237 sc->writeUpdate(ui, pb);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100238 endCpuCounter();
239
240 encodeTime += getCpuCounter();
241}
242
243void CConn::dataRect(const rfb::Rect &r, int encoding)
244{
Pierre Ossman9f273e92015-11-09 16:34:54 +0100245 CConnection::dataRect(r, encoding);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100246
247 if (encoding != rfb::encodingCopyRect) // FIXME
248 updates.add_changed(rfb::Region(r));
249}
250
251void CConn::setColourMapEntries(int, int, rdr::U16*)
252{
253}
254
255void CConn::bell()
256{
257}
258
259void CConn::serverCutText(const char*, rdr::U32)
260{
261}
262
263Manager::Manager(class rfb::SConnection *conn) :
264 EncodeManager(conn)
265{
266}
267
DRC77be9292015-02-21 11:44:26 -0600268void Manager::getStats(double& ratio, unsigned long long& encodedBytes,
269 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100270{
271 StatsVector::iterator iter;
272 unsigned long long bytes, equivalent;
273
274 bytes = equivalent = 0;
DRCb4c4a382015-02-21 11:28:37 -0600275 for (iter = stats.begin(); iter != stats.end(); ++iter) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100276 StatsVector::value_type::iterator iter2;
DRCb4c4a382015-02-21 11:28:37 -0600277 for (iter2 = iter->begin(); iter2 != iter->end(); ++iter2) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100278 bytes += iter2->bytes;
279 equivalent += iter2->equivalent;
280 }
281 }
282
DRC77be9292015-02-21 11:44:26 -0600283 ratio = (double)equivalent / bytes;
284 encodedBytes = bytes;
285 rawEquivalent = equivalent;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100286}
287
288SConn::SConn()
289{
290 out = new DummyOutStream;
291 setStreams(NULL, out);
292
293 setWriter(new rfb::SMsgWriter(&cp, out));
294
295 manager = new Manager(this);
296}
297
298SConn::~SConn()
299{
300 delete manager;
301 delete out;
302}
303
304void SConn::writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb)
305{
306 manager->writeUpdate(ui, pb, NULL);
307}
308
DRC77be9292015-02-21 11:44:26 -0600309void SConn::getStats(double& ratio, unsigned long long& bytes,
310 unsigned long long& rawEquivalent)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100311{
DRC77be9292015-02-21 11:44:26 -0600312 manager->getStats(ratio, bytes, rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100313}
314
315void SConn::setAccessRights(AccessRights ar)
316{
317}
318
319void SConn::setDesktopSize(int fb_width, int fb_height,
320 const rfb::ScreenSet& layout)
321{
322}
323
Pierre Ossman98d7af92015-11-16 09:37:46 +0100324struct stats
325{
326 double decodeTime;
327 double encodeTime;
328 double realTime;
329
330 double ratio;
331 unsigned long long bytes;
332 unsigned long long rawEquivalent;
333};
334
335static struct stats runTest(const char *fn)
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100336{
337 CConn *cc;
Pierre Ossman98d7af92015-11-16 09:37:46 +0100338 struct stats s;
339 struct timeval start, stop;
340
341 gettimeofday(&start, NULL);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100342
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100343 try {
DRC13cfb512015-02-26 12:24:03 -0600344 cc = new CConn(fn);
Pierre Ossman8ee522a2018-05-29 15:50:08 +0200345 } catch (rdr::Exception& e) {
Pierre Ossman86475a62015-03-03 16:42:15 +0100346 fprintf(stderr, "Failed to open rfb file: %s\n", e.str());
347 exit(1);
348 }
DRC13cfb512015-02-26 12:24:03 -0600349
Pierre Ossman86475a62015-03-03 16:42:15 +0100350 try {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100351 while (true)
352 cc->processMsg();
Pierre Ossman8ee522a2018-05-29 15:50:08 +0200353 } catch (rdr::EndOfStream& e) {
354 } catch (rdr::Exception& e) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100355 fprintf(stderr, "Failed to run rfb file: %s\n", e.str());
356 exit(1);
357 }
358
Pierre Ossman98d7af92015-11-16 09:37:46 +0100359 gettimeofday(&stop, NULL);
360
361 s.decodeTime = cc->decodeTime;
362 s.encodeTime = cc->encodeTime;
363 s.realTime = (double)stop.tv_sec - start.tv_sec;
364 s.realTime += ((double)stop.tv_usec - start.tv_usec)/1000000.0;
365 cc->getStats(s.ratio, s.bytes, s.rawEquivalent);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100366
367 delete cc;
368
Pierre Ossman98d7af92015-11-16 09:37:46 +0100369 return s;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100370}
371
372static void sort(double *array, int count)
373{
374 bool sorted;
375 int i;
376 do {
377 sorted = true;
DRCb4c4a382015-02-21 11:28:37 -0600378 for (i = 1; i < count; i++) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100379 if (array[i-1] > array[i]) {
380 double d;
381 d = array[i];
DRCb4c4a382015-02-21 11:28:37 -0600382 array[i] = array[i - 1];
383 array[i - 1] = d;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100384 sorted = false;
385 }
386 }
387 } while (!sorted);
388}
389
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100390static void usage(const char *argv0)
391{
392 fprintf(stderr, "Syntax: %s [options] <rfb file>\n", argv0);
393 fprintf(stderr, "Options:\n");
394 rfb::Configuration::listParams(79, 14);
395 exit(1);
396}
397
398int main(int argc, char **argv)
399{
400 int i;
401
402 const char *fn;
403
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100404 fn = NULL;
405 for (i = 1; i < argc; i++) {
406 if (rfb::Configuration::setParam(argv[i]))
407 continue;
408
409 if (argv[i][0] == '-') {
DRCb4c4a382015-02-21 11:28:37 -0600410 if (i + 1 < argc) {
411 if (rfb::Configuration::setParam(&argv[i][1], argv[i + 1])) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100412 i++;
413 continue;
414 }
415 }
416 usage(argv[0]);
417 }
418
419 if (fn != NULL)
420 usage(argv[0]);
421
422 fn = argv[i];
423 }
424
DRC4631a762015-02-21 11:57:27 -0600425 int runCount = count;
Pierre Ossman98d7af92015-11-16 09:37:46 +0100426 struct stats runs[runCount];
427 double values[runCount], dev[runCount];
428 double median, meddev;
DRC4631a762015-02-21 11:57:27 -0600429
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100430 if (fn == NULL) {
431 fprintf(stderr, "No file specified!\n\n");
432 usage(argv[0]);
433 }
434
Pierre Ossman135906e2015-04-27 12:48:47 +0200435 if (strcmp(format, "") == 0) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100436 fprintf(stderr, "Pixel format not specified!\n\n");
437 usage(argv[0]);
438 }
439
Pierre Ossman135906e2015-04-27 12:48:47 +0200440 if (width == 0 || height == 0) {
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100441 fprintf(stderr, "Frame buffer size not specified!\n\n");
442 usage(argv[0]);
443 }
444
445 // Warmup
Pierre Ossman98d7af92015-11-16 09:37:46 +0100446 runTest(fn);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100447
448 // Multiple runs to get a good average
DRCb4c4a382015-02-21 11:28:37 -0600449 for (i = 0; i < runCount; i++)
Pierre Ossman98d7af92015-11-16 09:37:46 +0100450 runs[i] = runTest(fn);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100451
Pierre Ossman98d7af92015-11-16 09:37:46 +0100452 // Calculate median and median deviation for CPU usage decoding
453 for (i = 0;i < runCount;i++)
454 values[i] = runs[i].decodeTime;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100455
Pierre Ossman98d7af92015-11-16 09:37:46 +0100456 sort(values, runCount);
457 median = values[runCount/2];
458
459 for (i = 0;i < runCount;i++)
460 dev[i] = fabs((values[i] - median) / median) * 100;
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100461
462 sort(dev, runCount);
Pierre Ossman98d7af92015-11-16 09:37:46 +0100463 meddev = dev[runCount/2];
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100464
Pierre Ossman98d7af92015-11-16 09:37:46 +0100465 printf("CPU time (decoding): %g s (+/- %g %%)\n", median, meddev);
466
467 // And for CPU usage encoding
468 for (i = 0;i < runCount;i++)
469 values[i] = runs[i].encodeTime;
470
471 sort(values, runCount);
472 median = values[runCount/2];
473
474 for (i = 0;i < runCount;i++)
475 dev[i] = fabs((values[i] - median) / median) * 100;
476
477 sort(dev, runCount);
478 meddev = dev[runCount/2];
479
480 printf("CPU time (encoding): %g s (+/- %g %%)\n", median, meddev);
481
482 // And for CPU core usage encoding
483 for (i = 0;i < runCount;i++)
484 values[i] = (runs[i].decodeTime + runs[i].encodeTime) / runs[i].realTime;
485
486 sort(values, runCount);
487 median = values[runCount/2];
488
489 for (i = 0;i < runCount;i++)
490 dev[i] = fabs((values[i] - median) / median) * 100;
491
492 sort(dev, runCount);
493 meddev = dev[runCount/2];
494
495 printf("Core usage (total): %g (+/- %g %%)\n", median, meddev);
496
Pierre Ossman7d218b02015-03-03 16:43:05 +0100497#ifdef WIN32
Pierre Ossman98d7af92015-11-16 09:37:46 +0100498 printf("Encoded bytes: %I64d\n", runs[0].bytes);
499 printf("Raw equivalent bytes: %I64d\n", runs[0].rawEquivalent);
Pierre Ossman7d218b02015-03-03 16:43:05 +0100500#else
Pierre Ossman98d7af92015-11-16 09:37:46 +0100501 printf("Encoded bytes: %lld\n", runs[0].bytes);
502 printf("Raw equivalent bytes: %lld\n", runs[0].rawEquivalent);
Pierre Ossman7d218b02015-03-03 16:43:05 +0100503#endif
Pierre Ossman98d7af92015-11-16 09:37:46 +0100504 printf("Ratio: %g\n", runs[0].ratio);
Pierre Ossman8738e8a2015-02-11 13:49:04 +0100505
506 return 0;
507}