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

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