Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 1 | /* Copyright 2015 Pierre Ossman <ossman@cendio.se> for Cendio AB |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 2 | * Copyright (C) 2015 D. R. Commander. All Rights Reserved. |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 3 | * |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 4 | * 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. |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 8 | * |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 9 | * 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. |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 13 | * |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 14 | * 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> |
| 32 | |
| 33 | #include <rdr/Exception.h> |
| 34 | #include <rdr/OutStream.h> |
| 35 | #include <rdr/FileInStream.h> |
| 36 | |
| 37 | #include <rfb/PixelFormat.h> |
| 38 | |
| 39 | #include <rfb/CConnection.h> |
| 40 | #include <rfb/CMsgReader.h> |
| 41 | #include <rfb/Decoder.h> |
| 42 | #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 | |
| 50 | static rfb::IntParameter width("width", "Frame buffer width", 0); |
| 51 | static rfb::IntParameter height("height", "Frame buffer height", 0); |
DRC | 4631a76 | 2015-02-21 11:57:27 -0600 | [diff] [blame^] | 52 | static rfb::IntParameter count("count", "Number of benchmark iterations", 9); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 53 | |
| 54 | static rfb::StringParameter format("format", "Pixel format (e.g. bgr888)", ""); |
| 55 | |
| 56 | // The frame buffer (and output) is always this format |
| 57 | static const rfb::PixelFormat fbPF(32, 24, false, true, 255, 255, 255, 0, 8, 16); |
| 58 | |
| 59 | // Encodings to use |
| 60 | static const rdr::S32 encodings[] = { |
| 61 | rfb::encodingTight, rfb::encodingCopyRect, rfb::encodingRRE, |
| 62 | rfb::encodingHextile, rfb::encodingZRLE, rfb::pseudoEncodingLastRect, |
| 63 | rfb::pseudoEncodingQualityLevel0 + 8 }; |
| 64 | |
| 65 | class DummyOutStream : public rdr::OutStream { |
| 66 | public: |
| 67 | DummyOutStream(); |
| 68 | |
| 69 | virtual int length(); |
| 70 | virtual void flush(); |
| 71 | |
| 72 | private: |
| 73 | virtual int overrun(int itemSize, int nItems); |
| 74 | |
| 75 | int offset; |
| 76 | rdr::U8 buf[131072]; |
| 77 | }; |
| 78 | |
| 79 | class CConn : public rfb::CConnection { |
| 80 | public: |
| 81 | CConn(const char *filename); |
| 82 | ~CConn(); |
| 83 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 84 | void getStats(double& ratio, unsigned long long& bytes, |
| 85 | unsigned long long& rawEquivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 86 | |
| 87 | virtual void setDesktopSize(int w, int h); |
| 88 | virtual void setCursor(int, int, const rfb::Point&, void*, void*); |
| 89 | virtual void framebufferUpdateStart(); |
| 90 | virtual void framebufferUpdateEnd(); |
| 91 | virtual void dataRect(const rfb::Rect&, int); |
| 92 | virtual void setColourMapEntries(int, int, rdr::U16*); |
| 93 | virtual void bell(); |
| 94 | virtual void serverCutText(const char*, rdr::U32); |
| 95 | |
| 96 | public: |
| 97 | double decodeTime; |
| 98 | double encodeTime; |
| 99 | |
| 100 | protected: |
| 101 | rdr::FileInStream *in; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 102 | rfb::Decoder *decoders[rfb::encodingMax + 1]; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 103 | rfb::ManagedPixelBuffer pb; |
| 104 | rfb::SimpleUpdateTracker updates; |
| 105 | class SConn *sc; |
| 106 | }; |
| 107 | |
| 108 | class Manager : public rfb::EncodeManager { |
| 109 | public: |
| 110 | Manager(class rfb::SConnection *conn); |
| 111 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 112 | void getStats(double&, unsigned long long&, unsigned long long&); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | class SConn : public rfb::SConnection { |
| 116 | public: |
| 117 | SConn(); |
| 118 | ~SConn(); |
| 119 | |
| 120 | void writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb); |
| 121 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 122 | void getStats(double&, unsigned long long&, unsigned long long&); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 123 | |
| 124 | virtual void setAccessRights(AccessRights ar); |
| 125 | |
| 126 | virtual void setDesktopSize(int fb_width, int fb_height, |
| 127 | const rfb::ScreenSet& layout); |
| 128 | |
| 129 | protected: |
| 130 | DummyOutStream *out; |
| 131 | Manager *manager; |
| 132 | }; |
| 133 | |
| 134 | DummyOutStream::DummyOutStream() |
| 135 | { |
| 136 | offset = 0; |
| 137 | ptr = buf; |
| 138 | end = buf + sizeof(buf); |
| 139 | } |
| 140 | |
| 141 | int DummyOutStream::length() |
| 142 | { |
| 143 | flush(); |
| 144 | return offset; |
| 145 | } |
| 146 | |
| 147 | void DummyOutStream::flush() |
| 148 | { |
| 149 | offset += ptr - buf; |
| 150 | ptr = buf; |
| 151 | } |
| 152 | |
| 153 | int DummyOutStream::overrun(int itemSize, int nItems) |
| 154 | { |
| 155 | flush(); |
| 156 | } |
| 157 | |
| 158 | CConn::CConn(const char *filename) |
| 159 | { |
| 160 | int i; |
| 161 | |
| 162 | decodeTime = 0.0; |
| 163 | encodeTime = 0.0; |
| 164 | |
| 165 | in = new rdr::FileInStream(filename); |
| 166 | setStreams(in, NULL); |
| 167 | |
| 168 | memset(decoders, 0, sizeof(decoders)); |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 169 | for (i = 0; i < rfb::encodingMax; i++) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 170 | if (!rfb::Decoder::supported(i)) |
| 171 | continue; |
| 172 | |
| 173 | decoders[i] = rfb::Decoder::createDecoder(i, this); |
| 174 | } |
| 175 | |
| 176 | pb.setPF(fbPF); |
| 177 | |
| 178 | // Need to skip the initial handshake and ServerInit |
| 179 | setState(RFBSTATE_NORMAL); |
| 180 | // That also means that the reader and writer weren't setup |
| 181 | setReader(new rfb::CMsgReader(this, in)); |
| 182 | // Nor the frame buffer size and format |
| 183 | setDesktopSize(width, height); |
| 184 | rfb::PixelFormat pf; |
| 185 | pf.parse(format); |
| 186 | setPixelFormat(pf); |
| 187 | |
| 188 | sc = new SConn(); |
| 189 | sc->cp.setPF(pb.getPF()); |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 190 | sc->setEncodings(sizeof(encodings) / sizeof(*encodings), encodings); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | CConn::~CConn() |
| 194 | { |
| 195 | int i; |
| 196 | |
| 197 | delete sc; |
| 198 | |
| 199 | delete in; |
| 200 | |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 201 | for (i = 0; i < rfb::encodingMax; i++) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 202 | delete decoders[i]; |
| 203 | } |
| 204 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 205 | void CConn::getStats(double& ratio, unsigned long long& bytes, |
| 206 | unsigned long long& rawEquivalent) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 207 | { |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 208 | sc->getStats(ratio, bytes, rawEquivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void CConn::setDesktopSize(int w, int h) |
| 212 | { |
| 213 | CConnection::setDesktopSize(w, h); |
| 214 | |
| 215 | pb.setSize(cp.width, cp.height); |
| 216 | } |
| 217 | |
| 218 | void CConn::setCursor(int, int, const rfb::Point&, void*, void*) |
| 219 | { |
| 220 | } |
| 221 | |
| 222 | void CConn::framebufferUpdateStart() |
| 223 | { |
| 224 | updates.clear(); |
| 225 | } |
| 226 | |
| 227 | void CConn::framebufferUpdateEnd() |
| 228 | { |
| 229 | rfb::UpdateInfo ui; |
| 230 | rfb::Region clip(pb.getRect()); |
| 231 | |
| 232 | updates.getUpdateInfo(&ui, clip); |
| 233 | |
| 234 | startCpuCounter(); |
| 235 | sc->writeUpdate(ui, &pb); |
| 236 | endCpuCounter(); |
| 237 | |
| 238 | encodeTime += getCpuCounter(); |
| 239 | } |
| 240 | |
| 241 | void CConn::dataRect(const rfb::Rect &r, int encoding) |
| 242 | { |
| 243 | if (!decoders[encoding]) |
| 244 | throw rdr::Exception("Unknown encoding"); |
| 245 | |
| 246 | startCpuCounter(); |
| 247 | decoders[encoding]->readRect(r, &pb); |
| 248 | endCpuCounter(); |
| 249 | |
| 250 | decodeTime += getCpuCounter(); |
| 251 | |
| 252 | if (encoding != rfb::encodingCopyRect) // FIXME |
| 253 | updates.add_changed(rfb::Region(r)); |
| 254 | } |
| 255 | |
| 256 | void CConn::setColourMapEntries(int, int, rdr::U16*) |
| 257 | { |
| 258 | } |
| 259 | |
| 260 | void CConn::bell() |
| 261 | { |
| 262 | } |
| 263 | |
| 264 | void CConn::serverCutText(const char*, rdr::U32) |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | Manager::Manager(class rfb::SConnection *conn) : |
| 269 | EncodeManager(conn) |
| 270 | { |
| 271 | } |
| 272 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 273 | void Manager::getStats(double& ratio, unsigned long long& encodedBytes, |
| 274 | unsigned long long& rawEquivalent) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 275 | { |
| 276 | StatsVector::iterator iter; |
| 277 | unsigned long long bytes, equivalent; |
| 278 | |
| 279 | bytes = equivalent = 0; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 280 | for (iter = stats.begin(); iter != stats.end(); ++iter) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 281 | StatsVector::value_type::iterator iter2; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 282 | for (iter2 = iter->begin(); iter2 != iter->end(); ++iter2) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 283 | bytes += iter2->bytes; |
| 284 | equivalent += iter2->equivalent; |
| 285 | } |
| 286 | } |
| 287 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 288 | ratio = (double)equivalent / bytes; |
| 289 | encodedBytes = bytes; |
| 290 | rawEquivalent = equivalent; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | SConn::SConn() |
| 294 | { |
| 295 | out = new DummyOutStream; |
| 296 | setStreams(NULL, out); |
| 297 | |
| 298 | setWriter(new rfb::SMsgWriter(&cp, out)); |
| 299 | |
| 300 | manager = new Manager(this); |
| 301 | } |
| 302 | |
| 303 | SConn::~SConn() |
| 304 | { |
| 305 | delete manager; |
| 306 | delete out; |
| 307 | } |
| 308 | |
| 309 | void SConn::writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb) |
| 310 | { |
| 311 | manager->writeUpdate(ui, pb, NULL); |
| 312 | } |
| 313 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 314 | void SConn::getStats(double& ratio, unsigned long long& bytes, |
| 315 | unsigned long long& rawEquivalent) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 316 | { |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 317 | manager->getStats(ratio, bytes, rawEquivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void SConn::setAccessRights(AccessRights ar) |
| 321 | { |
| 322 | } |
| 323 | |
| 324 | void SConn::setDesktopSize(int fb_width, int fb_height, |
| 325 | const rfb::ScreenSet& layout) |
| 326 | { |
| 327 | } |
| 328 | |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 329 | static double runTest(const char *fn, double& ratio, unsigned long long& bytes, |
| 330 | unsigned long long& rawEquivalent) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 331 | { |
| 332 | CConn *cc; |
| 333 | double time; |
| 334 | |
| 335 | cc = new CConn(fn); |
| 336 | |
| 337 | try { |
| 338 | while (true) |
| 339 | cc->processMsg(); |
| 340 | } catch (rdr::EndOfStream e) { |
| 341 | } catch (rdr::Exception e) { |
| 342 | fprintf(stderr, "Failed to run rfb file: %s\n", e.str()); |
| 343 | exit(1); |
| 344 | } |
| 345 | |
| 346 | time = cc->encodeTime; |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 347 | cc->getStats(ratio, bytes, rawEquivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 348 | |
| 349 | delete cc; |
| 350 | |
| 351 | return time; |
| 352 | } |
| 353 | |
| 354 | static void sort(double *array, int count) |
| 355 | { |
| 356 | bool sorted; |
| 357 | int i; |
| 358 | do { |
| 359 | sorted = true; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 360 | for (i = 1; i < count; i++) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 361 | if (array[i-1] > array[i]) { |
| 362 | double d; |
| 363 | d = array[i]; |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 364 | array[i] = array[i - 1]; |
| 365 | array[i - 1] = d; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 366 | sorted = false; |
| 367 | } |
| 368 | } |
| 369 | } while (!sorted); |
| 370 | } |
| 371 | |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 372 | static void usage(const char *argv0) |
| 373 | { |
| 374 | fprintf(stderr, "Syntax: %s [options] <rfb file>\n", argv0); |
| 375 | fprintf(stderr, "Options:\n"); |
| 376 | rfb::Configuration::listParams(79, 14); |
| 377 | exit(1); |
| 378 | } |
| 379 | |
| 380 | int main(int argc, char **argv) |
| 381 | { |
| 382 | int i; |
| 383 | |
| 384 | const char *fn; |
| 385 | |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 386 | fn = NULL; |
| 387 | for (i = 1; i < argc; i++) { |
| 388 | if (rfb::Configuration::setParam(argv[i])) |
| 389 | continue; |
| 390 | |
| 391 | if (argv[i][0] == '-') { |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 392 | if (i + 1 < argc) { |
| 393 | if (rfb::Configuration::setParam(&argv[i][1], argv[i + 1])) { |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 394 | i++; |
| 395 | continue; |
| 396 | } |
| 397 | } |
| 398 | usage(argv[0]); |
| 399 | } |
| 400 | |
| 401 | if (fn != NULL) |
| 402 | usage(argv[0]); |
| 403 | |
| 404 | fn = argv[i]; |
| 405 | } |
| 406 | |
DRC | 4631a76 | 2015-02-21 11:57:27 -0600 | [diff] [blame^] | 407 | int runCount = count; |
| 408 | double times[runCount], dev[runCount]; |
| 409 | double median, meddev, ratio; |
| 410 | unsigned long long bytes, equivalent; |
| 411 | |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 412 | if (fn == NULL) { |
| 413 | fprintf(stderr, "No file specified!\n\n"); |
| 414 | usage(argv[0]); |
| 415 | } |
| 416 | |
| 417 | if (!format.hasBeenSet()) { |
| 418 | fprintf(stderr, "Pixel format not specified!\n\n"); |
| 419 | usage(argv[0]); |
| 420 | } |
| 421 | |
| 422 | if (!width.hasBeenSet() || !height.hasBeenSet()) { |
| 423 | fprintf(stderr, "Frame buffer size not specified!\n\n"); |
| 424 | usage(argv[0]); |
| 425 | } |
| 426 | |
| 427 | // Warmup |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 428 | runTest(fn, ratio, bytes, equivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 429 | |
| 430 | // Multiple runs to get a good average |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 431 | for (i = 0; i < runCount; i++) |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 432 | times[i] = runTest(fn, ratio, bytes, equivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 433 | |
| 434 | // Calculate median and median deviation |
| 435 | sort(times, runCount); |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 436 | median = times[runCount / 2]; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 437 | |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 438 | for (i = 0; i < runCount; i++) |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 439 | dev[i] = fabs((times[i] - median) / median) * 100; |
| 440 | |
| 441 | sort(dev, runCount); |
DRC | b4c4a38 | 2015-02-21 11:28:37 -0600 | [diff] [blame] | 442 | meddev = dev[runCount / 2]; |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 443 | |
| 444 | printf("CPU time: %g s (+/- %g %)\n", median, meddev); |
DRC | 77be929 | 2015-02-21 11:44:26 -0600 | [diff] [blame] | 445 | printf("Encoded bytes: %lld\n", bytes); |
| 446 | printf("Raw equivalent bytes: %lld\n", equivalent); |
Pierre Ossman | 8738e8a | 2015-02-11 13:49:04 +0100 | [diff] [blame] | 447 | printf("Ratio: %g\n", ratio); |
| 448 | |
| 449 | return 0; |
| 450 | } |