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