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

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