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 11KB

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