PageRenderTime 187ms CodeModel.GetById 50ms app.highlight 99ms RepoModel.GetById 25ms app.codeStats 1ms

/apps/desktop/libvncserver/stats.c

http://ftk.googlecode.com/
C | 477 lines | 381 code | 64 blank | 32 comment | 69 complexity | 70e6e26aafe4943db01f3a8744621bc9 MD5 | raw file
  1/*
  2 * stats.c
  3 */
  4
  5/*
  6 *  Copyright (C) 2002 RealVNC Ltd.
  7 *  OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
  8 *  Original Xvnc code Copyright (C) 1999 AT&T Laboratories Cambridge.  
  9 *  All Rights Reserved.
 10 *
 11 *  This is free software; you can redistribute it and/or modify
 12 *  it under the terms of the GNU General Public License as published by
 13 *  the Free Software Foundation; either version 2 of the License, or
 14 *  (at your option) any later version.
 15 *
 16 *  This software is distributed in the hope that it will be useful,
 17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 *  GNU General Public License for more details.
 20 *
 21 *  You should have received a copy of the GNU General Public License
 22 *  along with this software; if not, write to the Free Software
 23 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 24 *  USA.
 25 */
 26
 27#include <rfb/rfb.h>
 28
 29char *messageNameServer2Client(uint32_t type, char *buf, int len);
 30char *messageNameClient2Server(uint32_t type, char *buf, int len);
 31char *encodingName(uint32_t enc, char *buf, int len);
 32
 33rfbStatList *rfbStatLookupEncoding(rfbClientPtr cl, uint32_t type);
 34rfbStatList *rfbStatLookupMessage(rfbClientPtr cl, uint32_t type);
 35
 36void  rfbStatRecordEncodingSent(rfbClientPtr cl, uint32_t type, int byteCount, int byteIfRaw);
 37void  rfbStatRecordEncodingRcvd(rfbClientPtr cl, uint32_t type, int byteCount, int byteIfRaw);
 38void  rfbStatRecordMessageSent(rfbClientPtr cl, uint32_t type, int byteCount, int byteIfRaw);
 39void  rfbStatRecordMessageRcvd(rfbClientPtr cl, uint32_t type, int byteCount, int byteIfRaw);
 40void rfbResetStats(rfbClientPtr cl);
 41void rfbPrintStats(rfbClientPtr cl);
 42
 43
 44
 45
 46char *messageNameServer2Client(uint32_t type, char *buf, int len) {
 47    if (buf==NULL) return "error";
 48    switch (type) {
 49    case rfbFramebufferUpdate:        snprintf(buf, len, "FramebufferUpdate"); break;
 50    case rfbSetColourMapEntries:      snprintf(buf, len, "SetColourMapEntries"); break;
 51    case rfbBell:                     snprintf(buf, len, "Bell"); break;
 52    case rfbServerCutText:            snprintf(buf, len, "ServerCutText"); break;
 53    case rfbResizeFrameBuffer:        snprintf(buf, len, "ResizeFrameBuffer"); break;
 54    case rfbKeyFrameUpdate:           snprintf(buf, len, "KeyFrameUpdate"); break;
 55    case rfbFileTransfer:             snprintf(buf, len, "FileTransfer"); break;
 56    case rfbTextChat:                 snprintf(buf, len, "TextChat"); break;
 57    case rfbPalmVNCReSizeFrameBuffer: snprintf(buf, len, "PalmVNCReSize"); break;
 58    default:
 59        snprintf(buf, len, "svr2cli-0x%08X", 0xFF);
 60    }
 61    return buf;
 62}
 63
 64char *messageNameClient2Server(uint32_t type, char *buf, int len) {
 65    if (buf==NULL) return "error";
 66    switch (type) {
 67    case rfbSetPixelFormat:           snprintf(buf, len, "SetPixelFormat"); break;
 68    case rfbFixColourMapEntries:      snprintf(buf, len, "FixColourMapEntries"); break;
 69    case rfbSetEncodings:             snprintf(buf, len, "SetEncodings"); break;
 70    case rfbFramebufferUpdateRequest: snprintf(buf, len, "FramebufferUpdate"); break;
 71    case rfbKeyEvent:                 snprintf(buf, len, "KeyEvent"); break;
 72    case rfbPointerEvent:             snprintf(buf, len, "PointerEvent"); break;
 73    case rfbClientCutText:            snprintf(buf, len, "ClientCutText"); break;
 74    case rfbFileTransfer:             snprintf(buf, len, "FileTransfer"); break;
 75    case rfbSetScale:                 snprintf(buf, len, "SetScale"); break;
 76    case rfbSetServerInput:           snprintf(buf, len, "SetServerInput"); break;
 77    case rfbSetSW:                    snprintf(buf, len, "SetSingleWindow"); break;
 78    case rfbTextChat:                 snprintf(buf, len, "TextChat"); break;
 79    case rfbKeyFrameRequest:          snprintf(buf, len, "KeyFrameRequest"); break;
 80    case rfbPalmVNCSetScaleFactor:    snprintf(buf, len, "PalmVNCSetScale"); break;
 81    default:
 82        snprintf(buf, len, "cli2svr-0x%08X", type);
 83
 84
 85    }
 86    return buf;
 87}
 88
 89/* Encoding name must be <=16 characters to fit nicely on the status output in
 90 * an 80 column terminal window
 91 */
 92char *encodingName(uint32_t type, char *buf, int len) {
 93    if (buf==NULL) return "error";
 94    
 95    switch (type) {
 96    case rfbEncodingRaw:                snprintf(buf, len, "raw");         break;
 97    case rfbEncodingCopyRect:           snprintf(buf, len, "copyRect");    break;
 98    case rfbEncodingRRE:                snprintf(buf, len, "RRE");         break;
 99    case rfbEncodingCoRRE:              snprintf(buf, len, "CoRRE");       break;
100    case rfbEncodingHextile:            snprintf(buf, len, "hextile");     break;
101    case rfbEncodingZlib:               snprintf(buf, len, "zlib");        break;
102    case rfbEncodingTight:              snprintf(buf, len, "tight");       break;
103    case rfbEncodingZlibHex:            snprintf(buf, len, "zlibhex");     break;
104    case rfbEncodingUltra:              snprintf(buf, len, "ultra");       break;
105    case rfbEncodingZRLE:               snprintf(buf, len, "ZRLE");        break;
106    case rfbEncodingCache:              snprintf(buf, len, "cache");       break;
107    case rfbEncodingCacheEnable:        snprintf(buf, len, "cacheEnable"); break;
108    case rfbEncodingXOR_Zlib:           snprintf(buf, len, "xorZlib");     break;
109    case rfbEncodingXORMonoColor_Zlib:  snprintf(buf, len, "xorMonoZlib");  break;
110    case rfbEncodingXORMultiColor_Zlib: snprintf(buf, len, "xorColorZlib"); break;
111    case rfbEncodingSolidColor:         snprintf(buf, len, "solidColor");  break;
112    case rfbEncodingXOREnable:          snprintf(buf, len, "xorEnable");   break;
113    case rfbEncodingCacheZip:           snprintf(buf, len, "cacheZip");    break;
114    case rfbEncodingSolMonoZip:         snprintf(buf, len, "monoZip");     break;
115    case rfbEncodingUltraZip:           snprintf(buf, len, "ultraZip");    break;
116
117    case rfbEncodingXCursor:            snprintf(buf, len, "Xcursor");     break;
118    case rfbEncodingRichCursor:         snprintf(buf, len, "RichCursor");  break;
119    case rfbEncodingPointerPos:         snprintf(buf, len, "PointerPos");  break;
120
121    case rfbEncodingLastRect:           snprintf(buf, len, "LastRect");    break;
122    case rfbEncodingNewFBSize:          snprintf(buf, len, "NewFBSize");   break;
123    case rfbEncodingKeyboardLedState:   snprintf(buf, len, "LedState");    break;
124    case rfbEncodingSupportedMessages:  snprintf(buf, len, "SupportedMessage");  break;
125    case rfbEncodingSupportedEncodings: snprintf(buf, len, "SupportedEncoding"); break;
126    case rfbEncodingServerIdentity:     snprintf(buf, len, "ServerIdentify");    break;
127
128    /* The following lookups do not report in stats */
129    case rfbEncodingCompressLevel0: snprintf(buf, len, "CompressLevel0");  break;
130    case rfbEncodingCompressLevel1: snprintf(buf, len, "CompressLevel1");  break;
131    case rfbEncodingCompressLevel2: snprintf(buf, len, "CompressLevel2");  break;
132    case rfbEncodingCompressLevel3: snprintf(buf, len, "CompressLevel3");  break;
133    case rfbEncodingCompressLevel4: snprintf(buf, len, "CompressLevel4");  break;
134    case rfbEncodingCompressLevel5: snprintf(buf, len, "CompressLevel5");  break;
135    case rfbEncodingCompressLevel6: snprintf(buf, len, "CompressLevel6");  break;
136    case rfbEncodingCompressLevel7: snprintf(buf, len, "CompressLevel7");  break;
137    case rfbEncodingCompressLevel8: snprintf(buf, len, "CompressLevel8");  break;
138    case rfbEncodingCompressLevel9: snprintf(buf, len, "CompressLevel9");  break;
139    
140    case rfbEncodingQualityLevel0:  snprintf(buf, len, "QualityLevel0");   break;
141    case rfbEncodingQualityLevel1:  snprintf(buf, len, "QualityLevel1");   break;
142    case rfbEncodingQualityLevel2:  snprintf(buf, len, "QualityLevel2");   break;
143    case rfbEncodingQualityLevel3:  snprintf(buf, len, "QualityLevel3");   break;
144    case rfbEncodingQualityLevel4:  snprintf(buf, len, "QualityLevel4");   break;
145    case rfbEncodingQualityLevel5:  snprintf(buf, len, "QualityLevel5");   break;
146    case rfbEncodingQualityLevel6:  snprintf(buf, len, "QualityLevel6");   break;
147    case rfbEncodingQualityLevel7:  snprintf(buf, len, "QualityLevel7");   break;
148    case rfbEncodingQualityLevel8:  snprintf(buf, len, "QualityLevel8");   break;
149    case rfbEncodingQualityLevel9:  snprintf(buf, len, "QualityLevel9");   break;
150
151
152    default:
153        snprintf(buf, len, "Enc(0x%08X)", type);
154    }
155
156    return buf;
157}
158
159
160
161
162
163rfbStatList *rfbStatLookupEncoding(rfbClientPtr cl, uint32_t type)
164{
165    rfbStatList *ptr;
166    if (cl==NULL) return NULL;
167    for (ptr = cl->statEncList; ptr!=NULL; ptr=ptr->Next)
168    {
169        if (ptr->type==type) return ptr;
170    }
171    /* Well, we are here... need to *CREATE* an entry */
172    ptr = (rfbStatList *)malloc(sizeof(rfbStatList));
173    if (ptr!=NULL)
174    {
175        memset((char *)ptr, 0, sizeof(rfbStatList));
176        ptr->type = type;
177        /* add to the top of the list */
178        ptr->Next = cl->statEncList;
179        cl->statEncList = ptr;
180    }
181    return ptr;
182}
183
184
185rfbStatList *rfbStatLookupMessage(rfbClientPtr cl, uint32_t type)
186{
187    rfbStatList *ptr;
188    if (cl==NULL) return NULL;
189    for (ptr = cl->statMsgList; ptr!=NULL; ptr=ptr->Next)
190    {
191        if (ptr->type==type) return ptr;
192    }
193    /* Well, we are here... need to *CREATE* an entry */
194    ptr = (rfbStatList *)malloc(sizeof(rfbStatList));
195    if (ptr!=NULL)
196    {
197        memset((char *)ptr, 0, sizeof(rfbStatList));
198        ptr->type = type;
199        /* add to the top of the list */
200        ptr->Next = cl->statMsgList;
201        cl->statMsgList = ptr;
202    }
203    return ptr;
204}
205
206void rfbStatRecordEncodingSentAdd(rfbClientPtr cl, uint32_t type, int byteCount) /* Specifically for tight encoding */
207{
208    rfbStatList *ptr;
209
210    ptr = rfbStatLookupEncoding(cl, type);
211    if (ptr!=NULL)
212        ptr->bytesSent      += byteCount;
213}
214
215
216void  rfbStatRecordEncodingSent(rfbClientPtr cl, uint32_t type, int byteCount, int byteIfRaw)
217{
218    rfbStatList *ptr;
219
220    ptr = rfbStatLookupEncoding(cl, type);
221    if (ptr!=NULL)
222    {
223        ptr->sentCount++;
224        ptr->bytesSent      += byteCount;
225        ptr->bytesSentIfRaw += byteIfRaw;
226    }
227}
228
229void  rfbStatRecordEncodingRcvd(rfbClientPtr cl, uint32_t type, int byteCount, int byteIfRaw)
230{
231    rfbStatList *ptr;
232
233    ptr = rfbStatLookupEncoding(cl, type);
234    if (ptr!=NULL)
235    {
236        ptr->rcvdCount++;
237        ptr->bytesRcvd      += byteCount;
238        ptr->bytesRcvdIfRaw += byteIfRaw;
239    }
240}
241
242void  rfbStatRecordMessageSent(rfbClientPtr cl, uint32_t type, int byteCount, int byteIfRaw)
243{
244    rfbStatList *ptr;
245
246    ptr = rfbStatLookupMessage(cl, type);
247    if (ptr!=NULL)
248    {
249        ptr->sentCount++;
250        ptr->bytesSent      += byteCount;
251        ptr->bytesSentIfRaw += byteIfRaw;
252    }
253}
254
255void  rfbStatRecordMessageRcvd(rfbClientPtr cl, uint32_t type, int byteCount, int byteIfRaw)
256{
257    rfbStatList *ptr;
258
259    ptr = rfbStatLookupMessage(cl, type);
260    if (ptr!=NULL)
261    {
262        ptr->rcvdCount++;
263        ptr->bytesRcvd      += byteCount;
264        ptr->bytesRcvdIfRaw += byteIfRaw;
265    }
266}
267
268
269int rfbStatGetSentBytes(rfbClientPtr cl)
270{
271    rfbStatList *ptr=NULL;
272    int bytes=0;
273    if (cl==NULL) return 0;
274    for (ptr = cl->statMsgList; ptr!=NULL; ptr=ptr->Next)
275        bytes += ptr->bytesSent;
276    for (ptr = cl->statEncList; ptr!=NULL; ptr=ptr->Next)
277        bytes += ptr->bytesSent;
278    return bytes;
279}
280
281int rfbStatGetSentBytesIfRaw(rfbClientPtr cl)
282{
283    rfbStatList *ptr=NULL;
284    int bytes=0;
285    if (cl==NULL) return 0;
286    for (ptr = cl->statMsgList; ptr!=NULL; ptr=ptr->Next)
287        bytes += ptr->bytesSentIfRaw;
288    for (ptr = cl->statEncList; ptr!=NULL; ptr=ptr->Next)
289        bytes += ptr->bytesSentIfRaw;
290    return bytes;
291}
292
293int rfbStatGetRcvdBytes(rfbClientPtr cl)
294{
295    rfbStatList *ptr=NULL;
296    int bytes=0;
297    if (cl==NULL) return 0;
298    for (ptr = cl->statMsgList; ptr!=NULL; ptr=ptr->Next)
299        bytes += ptr->bytesRcvd;
300    for (ptr = cl->statEncList; ptr!=NULL; ptr=ptr->Next)
301        bytes += ptr->bytesRcvd;
302    return bytes;
303}
304
305int rfbStatGetRcvdBytesIfRaw(rfbClientPtr cl)
306{
307    rfbStatList *ptr=NULL;
308    int bytes=0;
309    if (cl==NULL) return 0;
310    for (ptr = cl->statMsgList; ptr!=NULL; ptr=ptr->Next)
311        bytes += ptr->bytesRcvdIfRaw;
312    for (ptr = cl->statEncList; ptr!=NULL; ptr=ptr->Next)
313        bytes += ptr->bytesRcvdIfRaw;
314    return bytes;
315}
316
317int rfbStatGetMessageCountSent(rfbClientPtr cl, uint32_t type)
318{
319  rfbStatList *ptr=NULL;
320    if (cl==NULL) return 0;
321  for (ptr = cl->statMsgList; ptr!=NULL; ptr=ptr->Next)
322      if (ptr->type==type) return ptr->sentCount;
323  return 0;
324}
325int rfbStatGetMessageCountRcvd(rfbClientPtr cl, uint32_t type)
326{
327  rfbStatList *ptr=NULL;
328    if (cl==NULL) return 0;
329  for (ptr = cl->statMsgList; ptr!=NULL; ptr=ptr->Next)
330      if (ptr->type==type) return ptr->rcvdCount;
331  return 0;
332}
333
334int rfbStatGetEncodingCountSent(rfbClientPtr cl, uint32_t type)
335{
336  rfbStatList *ptr=NULL;
337    if (cl==NULL) return 0;
338  for (ptr = cl->statEncList; ptr!=NULL; ptr=ptr->Next)
339      if (ptr->type==type) return ptr->sentCount;
340  return 0;
341}
342int rfbStatGetEncodingCountRcvd(rfbClientPtr cl, uint32_t type)
343{
344  rfbStatList *ptr=NULL;
345    if (cl==NULL) return 0;
346  for (ptr = cl->statEncList; ptr!=NULL; ptr=ptr->Next)
347      if (ptr->type==type) return ptr->rcvdCount;
348  return 0;
349}
350
351
352
353
354void rfbResetStats(rfbClientPtr cl)
355{
356    rfbStatList *ptr;
357    if (cl==NULL) return;
358    while (cl->statEncList!=NULL)
359    {
360        ptr = cl->statEncList;
361        cl->statEncList = ptr->Next;
362        free(ptr);
363    }
364    while (cl->statMsgList!=NULL)
365    {
366        ptr = cl->statMsgList;
367        cl->statMsgList = ptr->Next;
368        free(ptr);
369    }
370}
371
372
373void rfbPrintStats(rfbClientPtr cl)
374{
375    rfbStatList *ptr=NULL;
376    char encBuf[64];
377    double savings=0.0;
378    int    totalRects=0;
379    double totalBytes=0.0;
380    double totalBytesIfRaw=0.0;
381
382    char *name=NULL;
383    int bytes=0;
384    int bytesIfRaw=0;
385    int count=0;
386
387    if (cl==NULL) return;
388    
389    rfbLog("%-21.21s  %-6.6s   %9.9s/%9.9s (%6.6s)\n", "Statistics", "events", "Transmit","RawEquiv","saved");
390    for (ptr = cl->statMsgList; ptr!=NULL; ptr=ptr->Next)
391    {
392        name       = messageNameServer2Client(ptr->type, encBuf, sizeof(encBuf));
393        count      = ptr->sentCount;
394        bytes      = ptr->bytesSent;
395        bytesIfRaw = ptr->bytesSentIfRaw;
396        
397        savings = 0.0;
398        if (bytesIfRaw>0.0)
399            savings = 100.0 - (((double)bytes / (double)bytesIfRaw) * 100.0);
400        if ((bytes>0) || (count>0) || (bytesIfRaw>0))
401            rfbLog(" %-20.20s: %6d | %9d/%9d (%5.1f%%)\n",
402	        name, count, bytes, bytesIfRaw, savings);
403        totalRects += count;
404        totalBytes += bytes;
405        totalBytesIfRaw += bytesIfRaw;
406    }
407
408    for (ptr = cl->statEncList; ptr!=NULL; ptr=ptr->Next)
409    {
410        name       = encodingName(ptr->type, encBuf, sizeof(encBuf));
411        count      = ptr->sentCount;
412        bytes      = ptr->bytesSent;
413        bytesIfRaw = ptr->bytesSentIfRaw;
414        savings    = 0.0;
415
416        if (bytesIfRaw>0.0)
417            savings = 100.0 - (((double)bytes / (double)bytesIfRaw) * 100.0);
418        if ((bytes>0) || (count>0) || (bytesIfRaw>0))
419            rfbLog(" %-20.20s: %6d | %9d/%9d (%5.1f%%)\n",
420	        name, count, bytes, bytesIfRaw, savings);
421        totalRects += count;
422        totalBytes += bytes;
423        totalBytesIfRaw += bytesIfRaw;
424    }
425    savings=0.0;
426    if (totalBytesIfRaw>0.0)
427        savings = 100.0 - ((totalBytes/totalBytesIfRaw)*100.0);
428    rfbLog(" %-20.20s: %6d | %9.0f/%9.0f (%5.1f%%)\n",
429            "TOTALS", totalRects, totalBytes,totalBytesIfRaw, savings);
430
431    totalRects=0.0;
432    totalBytes=0.0;
433    totalBytesIfRaw=0.0;
434
435    rfbLog("%-21.21s  %-6.6s   %9.9s/%9.9s (%6.6s)\n", "Statistics", "events", "Received","RawEquiv","saved");
436    for (ptr = cl->statMsgList; ptr!=NULL; ptr=ptr->Next)
437    {
438        name       = messageNameClient2Server(ptr->type, encBuf, sizeof(encBuf));
439        count      = ptr->rcvdCount;
440        bytes      = ptr->bytesRcvd;
441        bytesIfRaw = ptr->bytesRcvdIfRaw;
442        savings    = 0.0;
443
444        if (bytesIfRaw>0.0)
445            savings = 100.0 - (((double)bytes / (double)bytesIfRaw) * 100.0);
446        if ((bytes>0) || (count>0) || (bytesIfRaw>0))
447            rfbLog(" %-20.20s: %6d | %9d/%9d (%5.1f%%)\n",
448	        name, count, bytes, bytesIfRaw, savings);
449        totalRects += count;
450        totalBytes += bytes;
451        totalBytesIfRaw += bytesIfRaw;
452    }
453    for (ptr = cl->statEncList; ptr!=NULL; ptr=ptr->Next)
454    {
455        name       = encodingName(ptr->type, encBuf, sizeof(encBuf));
456        count      = ptr->rcvdCount;
457        bytes      = ptr->bytesRcvd;
458        bytesIfRaw = ptr->bytesRcvdIfRaw;
459        savings    = 0.0;
460
461        if (bytesIfRaw>0.0)
462            savings = 100.0 - (((double)bytes / (double)bytesIfRaw) * 100.0);
463        if ((bytes>0) || (count>0) || (bytesIfRaw>0))
464            rfbLog(" %-20.20s: %6d | %9d/%9d (%5.1f%%)\n",
465	        name, count, bytes, bytesIfRaw, savings);
466        totalRects += count;
467        totalBytes += bytes;
468        totalBytesIfRaw += bytesIfRaw;
469    }
470    savings=0.0;
471    if (totalBytesIfRaw>0.0)
472        savings = 100.0 - ((totalBytes/totalBytesIfRaw)*100.0);
473    rfbLog(" %-20.20s: %6d | %9.0f/%9.0f (%5.1f%%)\n",
474            "TOTALS", totalRects, totalBytes,totalBytesIfRaw, savings);
475      
476} 
477