You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

encperf.cxx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* Copyright 2015 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. * Copyright (C) 2015 D. R. Commander. All Rights Reserved.
  3. *
  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.
  8. *
  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.
  13. *
  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. * This program reads files produced by TightVNC's/TurboVNC's
  21. * fbs-dump, which in turn takes files from rfbproxy. It is
  22. * basically a dump of the RFB protocol from the server side after
  23. * the ServerInit message. Mostly this consists of FramebufferUpdate
  24. * message using the HexTile encoding. Screen size and pixel format
  25. * are not encoded in the file and must be specified by the user.
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <math.h>
  30. #include <sys/time.h>
  31. #include <rdr/Exception.h>
  32. #include <rdr/OutStream.h>
  33. #include <rdr/FileInStream.h>
  34. #include <rfb/PixelFormat.h>
  35. #include <rfb/CConnection.h>
  36. #include <rfb/CMsgReader.h>
  37. #include <rfb/CMsgWriter.h>
  38. #include <rfb/UpdateTracker.h>
  39. #include <rfb/EncodeManager.h>
  40. #include <rfb/SConnection.h>
  41. #include <rfb/SMsgWriter.h>
  42. #include "util.h"
  43. static rfb::IntParameter width("width", "Frame buffer width", 0);
  44. static rfb::IntParameter height("height", "Frame buffer height", 0);
  45. static rfb::IntParameter count("count", "Number of benchmark iterations", 9);
  46. static rfb::StringParameter format("format", "Pixel format (e.g. bgr888)", "");
  47. static rfb::BoolParameter translate("translate",
  48. "Translate 8-bit and 16-bit datasets into 24-bit",
  49. true);
  50. // The frame buffer (and output) is always this format
  51. static const rfb::PixelFormat fbPF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
  52. // Encodings to use
  53. static const rdr::S32 encodings[] = {
  54. rfb::encodingTight, rfb::encodingCopyRect, rfb::encodingRRE,
  55. rfb::encodingHextile, rfb::encodingZRLE, rfb::pseudoEncodingLastRect,
  56. rfb::pseudoEncodingQualityLevel0 + 8,
  57. rfb::pseudoEncodingCompressLevel0 + 2};
  58. class DummyOutStream : public rdr::OutStream {
  59. public:
  60. DummyOutStream();
  61. virtual size_t length();
  62. virtual void flush();
  63. private:
  64. virtual size_t overrun(size_t itemSize, size_t nItems);
  65. int offset;
  66. rdr::U8 buf[131072];
  67. };
  68. class CConn : public rfb::CConnection {
  69. public:
  70. CConn(const char *filename);
  71. ~CConn();
  72. void getStats(double& ratio, unsigned long long& bytes,
  73. unsigned long long& rawEquivalent);
  74. virtual void initDone() {};
  75. virtual void resizeFramebuffer();
  76. virtual void setCursor(int, int, const rfb::Point&, const rdr::U8*);
  77. virtual void framebufferUpdateStart();
  78. virtual void framebufferUpdateEnd();
  79. virtual void dataRect(const rfb::Rect&, int);
  80. virtual void setColourMapEntries(int, int, rdr::U16*);
  81. virtual void bell();
  82. virtual void serverCutText(const char*);
  83. public:
  84. double decodeTime;
  85. double encodeTime;
  86. protected:
  87. rdr::FileInStream *in;
  88. DummyOutStream *out;
  89. rfb::SimpleUpdateTracker updates;
  90. class SConn *sc;
  91. };
  92. class Manager : public rfb::EncodeManager {
  93. public:
  94. Manager(class rfb::SConnection *conn);
  95. void getStats(double&, unsigned long long&, unsigned long long&);
  96. };
  97. class SConn : public rfb::SConnection {
  98. public:
  99. SConn();
  100. ~SConn();
  101. void writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb);
  102. void getStats(double&, unsigned long long&, unsigned long long&);
  103. virtual void setAccessRights(AccessRights ar);
  104. virtual void setDesktopSize(int fb_width, int fb_height,
  105. const rfb::ScreenSet& layout);
  106. protected:
  107. DummyOutStream *out;
  108. Manager *manager;
  109. };
  110. DummyOutStream::DummyOutStream()
  111. {
  112. offset = 0;
  113. ptr = buf;
  114. end = buf + sizeof(buf);
  115. }
  116. size_t DummyOutStream::length()
  117. {
  118. flush();
  119. return offset;
  120. }
  121. void DummyOutStream::flush()
  122. {
  123. offset += ptr - buf;
  124. ptr = buf;
  125. }
  126. size_t DummyOutStream::overrun(size_t itemSize, size_t nItems)
  127. {
  128. flush();
  129. if (itemSize * nItems > (size_t)(end - ptr))
  130. nItems = (end - ptr) / itemSize;
  131. return nItems;
  132. }
  133. CConn::CConn(const char *filename)
  134. {
  135. decodeTime = 0.0;
  136. encodeTime = 0.0;
  137. in = new rdr::FileInStream(filename);
  138. out = new DummyOutStream;
  139. setStreams(in, out);
  140. // Need to skip the initial handshake and ServerInit
  141. setState(RFBSTATE_NORMAL);
  142. // That also means that the reader and writer weren't setup
  143. setReader(new rfb::CMsgReader(this, in));
  144. setWriter(new rfb::CMsgWriter(&server, out));
  145. // Nor the frame buffer size and format
  146. rfb::PixelFormat pf;
  147. pf.parse(format);
  148. setPixelFormat(pf);
  149. setDesktopSize(width, height);
  150. sc = new SConn();
  151. sc->client.setPF((bool)translate ? fbPF : pf);
  152. sc->setEncodings(sizeof(encodings) / sizeof(*encodings), encodings);
  153. }
  154. CConn::~CConn()
  155. {
  156. delete sc;
  157. delete in;
  158. delete out;
  159. }
  160. void CConn::getStats(double& ratio, unsigned long long& bytes,
  161. unsigned long long& rawEquivalent)
  162. {
  163. sc->getStats(ratio, bytes, rawEquivalent);
  164. }
  165. void CConn::resizeFramebuffer()
  166. {
  167. rfb::ModifiablePixelBuffer *pb;
  168. pb = new rfb::ManagedPixelBuffer((bool)translate ? fbPF : server.pf(),
  169. server.width(), server.height());
  170. setFramebuffer(pb);
  171. }
  172. void CConn::setCursor(int, int, const rfb::Point&, const rdr::U8*)
  173. {
  174. }
  175. void CConn::framebufferUpdateStart()
  176. {
  177. CConnection::framebufferUpdateStart();
  178. updates.clear();
  179. startCpuCounter();
  180. }
  181. void CConn::framebufferUpdateEnd()
  182. {
  183. rfb::UpdateInfo ui;
  184. rfb::PixelBuffer* pb = getFramebuffer();
  185. rfb::Region clip(pb->getRect());
  186. CConnection::framebufferUpdateEnd();
  187. endCpuCounter();
  188. decodeTime += getCpuCounter();
  189. updates.getUpdateInfo(&ui, clip);
  190. startCpuCounter();
  191. sc->writeUpdate(ui, pb);
  192. endCpuCounter();
  193. encodeTime += getCpuCounter();
  194. }
  195. void CConn::dataRect(const rfb::Rect &r, int encoding)
  196. {
  197. CConnection::dataRect(r, encoding);
  198. if (encoding != rfb::encodingCopyRect) // FIXME
  199. updates.add_changed(rfb::Region(r));
  200. }
  201. void CConn::setColourMapEntries(int, int, rdr::U16*)
  202. {
  203. }
  204. void CConn::bell()
  205. {
  206. }
  207. void CConn::serverCutText(const char*)
  208. {
  209. }
  210. Manager::Manager(class rfb::SConnection *conn) :
  211. EncodeManager(conn)
  212. {
  213. }
  214. void Manager::getStats(double& ratio, unsigned long long& encodedBytes,
  215. unsigned long long& rawEquivalent)
  216. {
  217. StatsVector::iterator iter;
  218. unsigned long long bytes, equivalent;
  219. bytes = equivalent = 0;
  220. for (iter = stats.begin(); iter != stats.end(); ++iter) {
  221. StatsVector::value_type::iterator iter2;
  222. for (iter2 = iter->begin(); iter2 != iter->end(); ++iter2) {
  223. bytes += iter2->bytes;
  224. equivalent += iter2->equivalent;
  225. }
  226. }
  227. ratio = (double)equivalent / bytes;
  228. encodedBytes = bytes;
  229. rawEquivalent = equivalent;
  230. }
  231. SConn::SConn()
  232. {
  233. out = new DummyOutStream;
  234. setStreams(NULL, out);
  235. setWriter(new rfb::SMsgWriter(&client, out));
  236. manager = new Manager(this);
  237. }
  238. SConn::~SConn()
  239. {
  240. delete manager;
  241. delete out;
  242. }
  243. void SConn::writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb)
  244. {
  245. manager->writeUpdate(ui, pb, NULL);
  246. }
  247. void SConn::getStats(double& ratio, unsigned long long& bytes,
  248. unsigned long long& rawEquivalent)
  249. {
  250. manager->getStats(ratio, bytes, rawEquivalent);
  251. }
  252. void SConn::setAccessRights(AccessRights ar)
  253. {
  254. }
  255. void SConn::setDesktopSize(int fb_width, int fb_height,
  256. const rfb::ScreenSet& layout)
  257. {
  258. }
  259. struct stats
  260. {
  261. double decodeTime;
  262. double encodeTime;
  263. double realTime;
  264. double ratio;
  265. unsigned long long bytes;
  266. unsigned long long rawEquivalent;
  267. };
  268. static struct stats runTest(const char *fn)
  269. {
  270. CConn *cc;
  271. struct stats s;
  272. struct timeval start, stop;
  273. gettimeofday(&start, NULL);
  274. try {
  275. cc = new CConn(fn);
  276. } catch (rdr::Exception& e) {
  277. fprintf(stderr, "Failed to open rfb file: %s\n", e.str());
  278. exit(1);
  279. }
  280. try {
  281. while (true)
  282. cc->processMsg();
  283. } catch (rdr::EndOfStream& e) {
  284. } catch (rdr::Exception& e) {
  285. fprintf(stderr, "Failed to run rfb file: %s\n", e.str());
  286. exit(1);
  287. }
  288. gettimeofday(&stop, NULL);
  289. s.decodeTime = cc->decodeTime;
  290. s.encodeTime = cc->encodeTime;
  291. s.realTime = (double)stop.tv_sec - start.tv_sec;
  292. s.realTime += ((double)stop.tv_usec - start.tv_usec)/1000000.0;
  293. cc->getStats(s.ratio, s.bytes, s.rawEquivalent);
  294. delete cc;
  295. return s;
  296. }
  297. static void sort(double *array, int count)
  298. {
  299. bool sorted;
  300. int i;
  301. do {
  302. sorted = true;
  303. for (i = 1; i < count; i++) {
  304. if (array[i-1] > array[i]) {
  305. double d;
  306. d = array[i];
  307. array[i] = array[i - 1];
  308. array[i - 1] = d;
  309. sorted = false;
  310. }
  311. }
  312. } while (!sorted);
  313. }
  314. static void usage(const char *argv0)
  315. {
  316. fprintf(stderr, "Syntax: %s [options] <rfb file>\n", argv0);
  317. fprintf(stderr, "Options:\n");
  318. rfb::Configuration::listParams(79, 14);
  319. exit(1);
  320. }
  321. int main(int argc, char **argv)
  322. {
  323. int i;
  324. const char *fn;
  325. fn = NULL;
  326. for (i = 1; i < argc; i++) {
  327. if (rfb::Configuration::setParam(argv[i]))
  328. continue;
  329. if (argv[i][0] == '-') {
  330. if (i + 1 < argc) {
  331. if (rfb::Configuration::setParam(&argv[i][1], argv[i + 1])) {
  332. i++;
  333. continue;
  334. }
  335. }
  336. usage(argv[0]);
  337. }
  338. if (fn != NULL)
  339. usage(argv[0]);
  340. fn = argv[i];
  341. }
  342. int runCount = count;
  343. struct stats *runs = new struct stats[runCount];
  344. double *values = new double[runCount];
  345. double *dev = new double[runCount];
  346. double median, meddev;
  347. if (fn == NULL) {
  348. fprintf(stderr, "No file specified!\n\n");
  349. usage(argv[0]);
  350. }
  351. if (strcmp(format, "") == 0) {
  352. fprintf(stderr, "Pixel format not specified!\n\n");
  353. usage(argv[0]);
  354. }
  355. if (width == 0 || height == 0) {
  356. fprintf(stderr, "Frame buffer size not specified!\n\n");
  357. usage(argv[0]);
  358. }
  359. // Warmup
  360. runTest(fn);
  361. // Multiple runs to get a good average
  362. for (i = 0; i < runCount; i++)
  363. runs[i] = runTest(fn);
  364. // Calculate median and median deviation for CPU usage decoding
  365. for (i = 0;i < runCount;i++)
  366. values[i] = runs[i].decodeTime;
  367. sort(values, runCount);
  368. median = values[runCount/2];
  369. for (i = 0;i < runCount;i++)
  370. dev[i] = fabs((values[i] - median) / median) * 100;
  371. sort(dev, runCount);
  372. meddev = dev[runCount/2];
  373. printf("CPU time (decoding): %g s (+/- %g %%)\n", median, meddev);
  374. // And for CPU usage encoding
  375. for (i = 0;i < runCount;i++)
  376. values[i] = runs[i].encodeTime;
  377. sort(values, runCount);
  378. median = values[runCount/2];
  379. for (i = 0;i < runCount;i++)
  380. dev[i] = fabs((values[i] - median) / median) * 100;
  381. sort(dev, runCount);
  382. meddev = dev[runCount/2];
  383. printf("CPU time (encoding): %g s (+/- %g %%)\n", median, meddev);
  384. // And for CPU core usage encoding
  385. for (i = 0;i < runCount;i++)
  386. values[i] = (runs[i].decodeTime + runs[i].encodeTime) / runs[i].realTime;
  387. sort(values, runCount);
  388. median = values[runCount/2];
  389. for (i = 0;i < runCount;i++)
  390. dev[i] = fabs((values[i] - median) / median) * 100;
  391. sort(dev, runCount);
  392. meddev = dev[runCount/2];
  393. printf("Core usage (total): %g (+/- %g %%)\n", median, meddev);
  394. #ifdef WIN32
  395. printf("Encoded bytes: %I64d\n", runs[0].bytes);
  396. printf("Raw equivalent bytes: %I64d\n", runs[0].rawEquivalent);
  397. #else
  398. printf("Encoded bytes: %lld\n", runs[0].bytes);
  399. printf("Raw equivalent bytes: %lld\n", runs[0].rawEquivalent);
  400. #endif
  401. printf("Ratio: %g\n", runs[0].ratio);
  402. return 0;
  403. }