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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 void overrun(size_t needed);
  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 setCursorPos(const rfb::Point&);
  79. virtual void framebufferUpdateStart();
  80. virtual void framebufferUpdateEnd();
  81. virtual bool dataRect(const rfb::Rect&, int);
  82. virtual void setColourMapEntries(int, int, rdr::U16*);
  83. virtual void bell();
  84. virtual void serverCutText(const char*);
  85. public:
  86. double decodeTime;
  87. double encodeTime;
  88. protected:
  89. rdr::FileInStream *in;
  90. DummyOutStream *out;
  91. rfb::SimpleUpdateTracker updates;
  92. class SConn *sc;
  93. };
  94. class Manager : public rfb::EncodeManager {
  95. public:
  96. Manager(class rfb::SConnection *conn);
  97. void getStats(double&, unsigned long long&, unsigned long long&);
  98. };
  99. class SConn : public rfb::SConnection {
  100. public:
  101. SConn();
  102. ~SConn();
  103. void writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb);
  104. void getStats(double&, unsigned long long&, unsigned long long&);
  105. virtual void setAccessRights(AccessRights ar);
  106. virtual void setDesktopSize(int fb_width, int fb_height,
  107. const rfb::ScreenSet& layout);
  108. protected:
  109. DummyOutStream *out;
  110. Manager *manager;
  111. };
  112. DummyOutStream::DummyOutStream()
  113. {
  114. offset = 0;
  115. ptr = buf;
  116. end = buf + sizeof(buf);
  117. }
  118. size_t DummyOutStream::length()
  119. {
  120. flush();
  121. return offset;
  122. }
  123. void DummyOutStream::flush()
  124. {
  125. offset += ptr - buf;
  126. ptr = buf;
  127. }
  128. void DummyOutStream::overrun(size_t needed)
  129. {
  130. flush();
  131. if (avail() < needed)
  132. throw rdr::Exception("Insufficient dummy output buffer");
  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::setCursorPos(const rfb::Point&)
  177. {
  178. }
  179. void CConn::framebufferUpdateStart()
  180. {
  181. CConnection::framebufferUpdateStart();
  182. updates.clear();
  183. startCpuCounter();
  184. }
  185. void CConn::framebufferUpdateEnd()
  186. {
  187. rfb::UpdateInfo ui;
  188. rfb::PixelBuffer* pb = getFramebuffer();
  189. rfb::Region clip(pb->getRect());
  190. CConnection::framebufferUpdateEnd();
  191. endCpuCounter();
  192. decodeTime += getCpuCounter();
  193. updates.getUpdateInfo(&ui, clip);
  194. startCpuCounter();
  195. sc->writeUpdate(ui, pb);
  196. endCpuCounter();
  197. encodeTime += getCpuCounter();
  198. }
  199. bool CConn::dataRect(const rfb::Rect &r, int encoding)
  200. {
  201. if (!CConnection::dataRect(r, encoding))
  202. return false;
  203. if (encoding != rfb::encodingCopyRect) // FIXME
  204. updates.add_changed(rfb::Region(r));
  205. return true;
  206. }
  207. void CConn::setColourMapEntries(int, int, rdr::U16*)
  208. {
  209. }
  210. void CConn::bell()
  211. {
  212. }
  213. void CConn::serverCutText(const char*)
  214. {
  215. }
  216. Manager::Manager(class rfb::SConnection *conn) :
  217. EncodeManager(conn)
  218. {
  219. }
  220. void Manager::getStats(double& ratio, unsigned long long& encodedBytes,
  221. unsigned long long& rawEquivalent)
  222. {
  223. StatsVector::iterator iter;
  224. unsigned long long bytes, equivalent;
  225. bytes = equivalent = 0;
  226. for (iter = stats.begin(); iter != stats.end(); ++iter) {
  227. StatsVector::value_type::iterator iter2;
  228. for (iter2 = iter->begin(); iter2 != iter->end(); ++iter2) {
  229. bytes += iter2->bytes;
  230. equivalent += iter2->equivalent;
  231. }
  232. }
  233. ratio = (double)equivalent / bytes;
  234. encodedBytes = bytes;
  235. rawEquivalent = equivalent;
  236. }
  237. SConn::SConn()
  238. {
  239. out = new DummyOutStream;
  240. setStreams(NULL, out);
  241. setWriter(new rfb::SMsgWriter(&client, out));
  242. manager = new Manager(this);
  243. }
  244. SConn::~SConn()
  245. {
  246. delete manager;
  247. delete out;
  248. }
  249. void SConn::writeUpdate(const rfb::UpdateInfo& ui, const rfb::PixelBuffer* pb)
  250. {
  251. manager->writeUpdate(ui, pb, NULL);
  252. }
  253. void SConn::getStats(double& ratio, unsigned long long& bytes,
  254. unsigned long long& rawEquivalent)
  255. {
  256. manager->getStats(ratio, bytes, rawEquivalent);
  257. }
  258. void SConn::setAccessRights(AccessRights ar)
  259. {
  260. }
  261. void SConn::setDesktopSize(int fb_width, int fb_height,
  262. const rfb::ScreenSet& layout)
  263. {
  264. }
  265. struct stats
  266. {
  267. double decodeTime;
  268. double encodeTime;
  269. double realTime;
  270. double ratio;
  271. unsigned long long bytes;
  272. unsigned long long rawEquivalent;
  273. };
  274. static struct stats runTest(const char *fn)
  275. {
  276. CConn *cc;
  277. struct stats s;
  278. struct timeval start, stop;
  279. gettimeofday(&start, NULL);
  280. try {
  281. cc = new CConn(fn);
  282. } catch (rdr::Exception& e) {
  283. fprintf(stderr, "Failed to open rfb file: %s\n", e.str());
  284. exit(1);
  285. }
  286. try {
  287. while (true)
  288. cc->processMsg();
  289. } catch (rdr::EndOfStream& e) {
  290. } catch (rdr::Exception& e) {
  291. fprintf(stderr, "Failed to run rfb file: %s\n", e.str());
  292. exit(1);
  293. }
  294. gettimeofday(&stop, NULL);
  295. s.decodeTime = cc->decodeTime;
  296. s.encodeTime = cc->encodeTime;
  297. s.realTime = (double)stop.tv_sec - start.tv_sec;
  298. s.realTime += ((double)stop.tv_usec - start.tv_usec)/1000000.0;
  299. cc->getStats(s.ratio, s.bytes, s.rawEquivalent);
  300. delete cc;
  301. return s;
  302. }
  303. static void sort(double *array, int count)
  304. {
  305. bool sorted;
  306. int i;
  307. do {
  308. sorted = true;
  309. for (i = 1; i < count; i++) {
  310. if (array[i-1] > array[i]) {
  311. double d;
  312. d = array[i];
  313. array[i] = array[i - 1];
  314. array[i - 1] = d;
  315. sorted = false;
  316. }
  317. }
  318. } while (!sorted);
  319. }
  320. static void usage(const char *argv0)
  321. {
  322. fprintf(stderr, "Syntax: %s [options] <rfb file>\n", argv0);
  323. fprintf(stderr, "Options:\n");
  324. rfb::Configuration::listParams(79, 14);
  325. exit(1);
  326. }
  327. int main(int argc, char **argv)
  328. {
  329. int i;
  330. const char *fn;
  331. fn = NULL;
  332. for (i = 1; i < argc; i++) {
  333. if (rfb::Configuration::setParam(argv[i]))
  334. continue;
  335. if (argv[i][0] == '-') {
  336. if (i + 1 < argc) {
  337. if (rfb::Configuration::setParam(&argv[i][1], argv[i + 1])) {
  338. i++;
  339. continue;
  340. }
  341. }
  342. usage(argv[0]);
  343. }
  344. if (fn != NULL)
  345. usage(argv[0]);
  346. fn = argv[i];
  347. }
  348. int runCount = count;
  349. struct stats *runs = new struct stats[runCount];
  350. double *values = new double[runCount];
  351. double *dev = new double[runCount];
  352. double median, meddev;
  353. if (fn == NULL) {
  354. fprintf(stderr, "No file specified!\n\n");
  355. usage(argv[0]);
  356. }
  357. if (strcmp(format, "") == 0) {
  358. fprintf(stderr, "Pixel format not specified!\n\n");
  359. usage(argv[0]);
  360. }
  361. if (width == 0 || height == 0) {
  362. fprintf(stderr, "Frame buffer size not specified!\n\n");
  363. usage(argv[0]);
  364. }
  365. // Warmup
  366. runTest(fn);
  367. // Multiple runs to get a good average
  368. for (i = 0; i < runCount; i++)
  369. runs[i] = runTest(fn);
  370. // Calculate median and median deviation for CPU usage decoding
  371. for (i = 0;i < runCount;i++)
  372. values[i] = runs[i].decodeTime;
  373. sort(values, runCount);
  374. median = values[runCount/2];
  375. for (i = 0;i < runCount;i++)
  376. dev[i] = fabs((values[i] - median) / median) * 100;
  377. sort(dev, runCount);
  378. meddev = dev[runCount/2];
  379. printf("CPU time (decoding): %g s (+/- %g %%)\n", median, meddev);
  380. // And for CPU usage encoding
  381. for (i = 0;i < runCount;i++)
  382. values[i] = runs[i].encodeTime;
  383. sort(values, runCount);
  384. median = values[runCount/2];
  385. for (i = 0;i < runCount;i++)
  386. dev[i] = fabs((values[i] - median) / median) * 100;
  387. sort(dev, runCount);
  388. meddev = dev[runCount/2];
  389. printf("CPU time (encoding): %g s (+/- %g %%)\n", median, meddev);
  390. // And for CPU core usage encoding
  391. for (i = 0;i < runCount;i++)
  392. values[i] = (runs[i].decodeTime + runs[i].encodeTime) / runs[i].realTime;
  393. sort(values, runCount);
  394. median = values[runCount/2];
  395. for (i = 0;i < runCount;i++)
  396. dev[i] = fabs((values[i] - median) / median) * 100;
  397. sort(dev, runCount);
  398. meddev = dev[runCount/2];
  399. printf("Core usage (total): %g (+/- %g %%)\n", median, meddev);
  400. printf("Encoded bytes: %llu\n", runs[0].bytes);
  401. printf("Raw equivalent bytes: %llu\n", runs[0].rawEquivalent);
  402. printf("Ratio: %g\n", runs[0].ratio);
  403. return 0;
  404. }