Ignore:
Timestamp:
Sep 24, 2008, 10:03:52 AM (16 years ago)
Author:
gah
Message:

added stats counters

Location:
trunk/packages/vizservers
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanovis/Axis.h

    r1111 r1161  
    262262    double InvMap(double x);
    263263    const char *name(void) {
    264         return (name_ == NULL) ? "???" : name_;
     264        return name_;
    265265    }
    266266    void name(const char *name) {
     
    271271    }
    272272    const char *units(void) {
    273         return (units_ == NULL) ? "???" : units_;
     273        return units_;
    274274    }
    275275    void units(const char *units) {
     
    280280    }
    281281    const char *title(void) {
    282         return (title_ == NULL) ? "???" : title_;
     282        return title_;
    283283    }
    284284    void title(const char *title) {
  • trunk/packages/vizservers/nanovis/AxisRange.h

    r1111 r1161  
    22#define _AXIS_RANGE_H
    33
     4#include <string.h>
     5
    46class AxisRange {
    57    double min_, max_;
     8    char *units_;
    69public:
    710    AxisRange(void) {
    811        min(0.0), max(1.0);
     12    }
     13    ~AxisRange(void) {
     14        if (units_ != NULL) {
     15            delete [] units_;
     16        }
    917    }
    1018    void SetRange(double min, double max) {
     
    2331        max_ = max;
    2432    }
     33    const char *units(void) {
     34        return units_;
     35    }
     36    void units(const char *units) {
     37        if (units_ != NULL) {
     38            delete [] units_;
     39        }
     40        units_ = new char [strlen(units) + 1];
     41        strcpy(units_, units);
     42    }
    2543};
    2644
  • trunk/packages/vizservers/nanovis/Command.cpp

    r1156 r1161  
    104104
    105105// Tcl interpreter for incoming messages
    106 static Tcl_Interp *interp;
    107 static Tcl_DString cmdbuffer;
    108106
    109107// default transfer function
     
    453451                return TCL_ERROR;
    454452            }
    455             if (index < NanoVis::volume.size() && NanoVis::volume[index] != NULL) {
     453            if (NanoVis::volume[index] != NULL) {
    456454                vectorPtr->push_back(index);
    457455            }
     
    19141912}
    19151913
    1916 
    19171914static int
    19181915HeightMapDataFollowsOp(ClientData cdata, Tcl_Interp *interp, int objc,
     
    21162113    return TCL_OK;
    21172114}
     2115
    21182116
    21192117static int
     
    24752473    float xMin, yMin, xMax, yMax;
    24762474    float *zValues;
     2475    const char *xUnits, *yUnits, *zUnits;
    24772476
    24782477    if ((objc & 0x01) == 0) {
     
    24852484    xNum = yNum = zNum = 0;
    24862485    xMin = yMin = xMax = yMax = 0.0f;
     2486    xUnits = yUnits = zUnits = NULL;
    24872487    int i;
    24882488    for (i = 1; i < objc; i += 2) {
    24892489        const char *string;
     2490        char c;
    24902491
    24912492        string = Tcl_GetString(objv[i]);
    2492         if (strcmp(string, "xmin") == 0) {
    2493             if (GetFloatFromObj(interp, objv[i+1], &xMin) != TCL_OK) {
    2494                 return TCL_ERROR;
    2495             }
    2496         } else if (strcmp(string, "xmax") == 0) {
    2497             if (GetFloatFromObj(interp, objv[i+1], &xMax) != TCL_OK) {
    2498                 return TCL_ERROR;
    2499             }
    2500         } else if (strcmp(string, "xnum") == 0) {
    2501             if (Tcl_GetIntFromObj(interp, objv[i+1], &xNum) != TCL_OK) {
    2502                 return TCL_ERROR;
    2503             }
    2504             if (xNum <= 0) {
    2505                 Tcl_AppendResult(interp, "bad xnum value: must be > 0",
    2506                                  (char *)NULL);
    2507                 return TCL_ERROR;
    2508             }
    2509         } else if (strcmp(string, "ymin") == 0) {
     2493        c = string[0];
     2494        if ((c == 'x') && (strcmp(string, "xmin") == 0)) {
     2495            if (GetFloatFromObj(interp, objv[i+1], &xMin) != TCL_OK) {
     2496                return TCL_ERROR;
     2497            }
     2498        } else if ((c == 'x') && (strcmp(string, "xmax") == 0)) {
     2499            if (GetFloatFromObj(interp, objv[i+1], &xMax) != TCL_OK) {
     2500                return TCL_ERROR;
     2501            }
     2502        } else if ((c == 'x') && (strcmp(string, "xnum") == 0)) {
     2503            if (Tcl_GetIntFromObj(interp, objv[i+1], &xNum) != TCL_OK) {
     2504                return TCL_ERROR;
     2505            }
     2506            if (xNum <= 0) {
     2507                Tcl_AppendResult(interp, "bad xnum value: must be > 0",
     2508                                 (char *)NULL);
     2509                return TCL_ERROR;
     2510            }
     2511        } else if ((c == 'x') && (strcmp(string, "xunits") == 0)) {
     2512            xUnits = Tcl_GetString(objv[i+1]);
     2513        } else if ((c == 'y') && (strcmp(string, "ymin") == 0)) {
    25102514            if (GetFloatFromObj(interp, objv[i+1], &yMin) != TCL_OK) {
    25112515                return TCL_ERROR;
    25122516            }
    2513         } else if (strcmp(string, "ymax") == 0) {
     2517        } else if ((c == 'y') && (strcmp(string, "ymax") == 0)) {
    25142518            if (GetFloatFromObj(interp, objv[i+1], &yMax) != TCL_OK) {
    25152519                return TCL_ERROR;
    25162520            }
    2517         } else if (strcmp(string, "ynum") == 0) {
     2521        } else if ((c == 'y') && (strcmp(string, "ynum") == 0)) {
    25182522            if (Tcl_GetIntFromObj(interp, objv[i+1], &yNum) != TCL_OK) {
    25192523                return TCL_ERROR;
     
    25242528                return TCL_ERROR;
    25252529            }
    2526         } else if (strcmp(string, "zvalues") == 0) {
     2530        } else if ((c == 'y') && (strcmp(string, "yunits") == 0)) {
     2531            yUnits = Tcl_GetString(objv[i+1]);
     2532        } else if ((c == 'z') && (strcmp(string, "zvalues") == 0)) {
    25272533            Tcl_Obj **zObj;
    25282534
     
    25372543                }
    25382544            }
     2545        } else if ((c == 'z') && (strcmp(string, "zunits") == 0)) {
     2546            zUnits = Tcl_GetString(objv[i+1]);
    25392547        } else {
    25402548            Tcl_AppendResult(interp, "unknown key \"", string,
    2541                 "\": should be xmin, xmax, xnum, ymin, ymax, ynum, or zvalues",
     2549                "\": should be xmin, xmax, xnum, xunits, ymin, ymax, ynum, yunits, zvalues, or zunits",
    25422550                (char *)NULL);
    25432551            return TCL_ERROR;
     
    25562564    hmPtr = new HeightMap();
    25572565
     2566    // Must set units before the heights.
     2567    hmPtr->xAxis.units(xUnits);
     2568    hmPtr->yAxis.units(yUnits);
     2569    hmPtr->zAxis.units(zUnits);
     2570    hmPtr->wAxis.units(yUnits);
    25582571    hmPtr->setHeight(xMin, yMin, xMax, yMax, xNum, yNum, zValues);
    25592572    hmPtr->setColorMap(NanoVis::get_transfunc("default"));
     
    25652578
    25662579
    2567 void
     2580Tcl_Interp *
    25682581initTcl()
    25692582{
     2583
    25702584    /*
    25712585     * Ideally the connection is authenticated by nanoscale.  I still like the
     
    25752589     * a test harness through the interpreter for nanovis.
    25762590     */
     2591    Tcl_Interp *interp;
    25772592    interp = Tcl_CreateInterp();
    25782593    Tcl_MakeSafe(interp);
    2579 
    2580     Tcl_DStringInit(&cmdbuffer);
    25812594
    25822595    Tcl_CreateObjCommand(interp, "axis",        AxisCmd,        NULL, NULL);
     
    26022615        fprintf(NanoVis::logfile, Tcl_GetStringResult(interp));
    26032616    }
    2604 }
    2605 
    2606 
    2607 void
    2608 xinetd_listen()
    2609 {
    2610     int flags = fcntl(0, F_GETFL, 0);
    2611     fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
    2612 
    2613     int status = TCL_OK;
    2614     int npass = 0;
    2615 
    2616     //
    2617     //  Read and execute as many commands as we can from stdin...
    2618     //
    2619     while (status == TCL_OK) {
    2620         //
    2621         //  Read the next command from the buffer.  First time through we
    2622         //  block here and wait if necessary until a command comes in.
    2623         //
    2624         //  BE CAREFUL: Read only one command, up to a newline.  The "volume
    2625         //  data follows" command needs to be able to read the data
    2626         //  immediately following the command, and we shouldn't consume it
    2627         //  here.
    2628         //
    2629         while (1) {
    2630             int c = fgetc(NanoVis::stdin);
    2631             char ch;
    2632             if (c <= 0) {
    2633                 if (npass == 0) {
    2634                     exit(0);  // EOF -- we're done!
    2635                 } else {
    2636                     break;
    2637                 }
    2638             }
    2639             ch = (char)c;
    2640             Tcl_DStringAppend(&cmdbuffer, &ch, 1);
    2641 
    2642             if (ch=='\n' && Tcl_CommandComplete(Tcl_DStringValue(&cmdbuffer))) {
    2643                 break;
    2644             }
    2645         }
    2646         // no command? then we're done for now
    2647         if (Tcl_DStringLength(&cmdbuffer) == 0) {
    2648             break;
    2649         }
    2650 
    2651         // back to original flags during command evaluation...
    2652         fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
    2653         if (debug_flag) {
    2654             fprintf(NanoVis::logfile, "%s\n", Tcl_DStringValue(&cmdbuffer));
    2655         }
    2656         status = Tcl_Eval(interp, Tcl_DStringValue(&cmdbuffer));
    2657         Tcl_DStringSetLength(&cmdbuffer, 0);
    2658 
    2659         // non-blocking for next read -- we might not get anything
    2660         fcntl(0, F_SETFL, flags | O_NONBLOCK);
    2661         npass++;
    2662     }
    2663     fcntl(0, F_SETFL, flags);
    2664 
    2665     if (status != TCL_OK) {
    2666         const char *string;
    2667         int nBytes;
    2668 
    2669         string = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), &nBytes);
    2670         struct iovec iov[3];
    2671         iov[0].iov_base = (char *)"NanoVis Server Error: ";
    2672         iov[0].iov_len = strlen((char *)iov[0].iov_base);
    2673         iov[1].iov_base = (char *)string;
    2674         iov[1].iov_len = nBytes;
    2675         iov[2].iov_base = (char *)'\n';
    2676         iov[2].iov_len = 1;
    2677         writev(0, iov, 3);
    2678         return;
    2679     }
    2680 
    2681     //
    2682     // This is now in "FlowCmd()":
    2683     //  Generate the latest frame and send it back to the client
    2684     //
    2685     /*
    2686       if (NanoVis::licRenderer && NanoVis::licRenderer->isActivated())
    2687       {
    2688       NanoVis::licRenderer->convolve();
    2689       }
    2690 
    2691       if (NanoVis::particleRenderer && NanoVis::particleRenderer->isActivated())
    2692       {
    2693       NanoVis::particleRenderer->advect();
    2694       }
    2695     */
    2696 
    2697     NanoVis::update();
    2698 
    2699     NanoVis::offscreen_buffer_capture();  //enable offscreen render
    2700 
    2701     NanoVis::display();
     2617    return interp;
     2618}
     2619
     2620
     2621
     2622int
     2623NanoVis::render_2d_contour(HeightMap* heightmap, int width, int height)
     2624{
     2625    int old_width = win_width;
     2626    int old_height = win_height;
     2627
     2628    resize_offscreen_buffer(width, height);
     2629
     2630/* plane_render->set_screen_size(width, height);
     2631
     2632    // generate data for the legend
     2633    float data[512];
     2634    for (int i=0; i < 256; i++) {
     2635        data[i] = data[i+256] = (float)(i/255.0);
     2636    }
     2637    plane[0] = new Texture2D(256, 2, GL_FLOAT, GL_LINEAR, 1, data);
     2638    int index = plane_render->add_plane(plane[0], tf);
     2639    plane_render->set_active_plane(index);
     2640
     2641    offscreen_buffer_capture();
     2642    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear screen
     2643
     2644    //plane_render->render();
     2645    // INSOO : is going to implement here for the topview of the heightmap
     2646    heightmap->render(renderContext);
    27022647
    27032648    // INSOO
    2704 #ifdef XINETD
    2705     NanoVis::read_screen();
    2706     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    2707 #else
    2708     NanoVis::display_offscreen_buffer(); //display the final rendering on screen
    2709     NanoVis::read_screen();
    2710     glutSwapBuffers();
    2711 #endif
    2712 
    2713 #if DO_RLE
    2714     do_rle();
    2715     int sizes[2] = {  offsets_size*sizeof(offsets[0]), rle_size };
    2716     fprintf(stderr, "Writing %d,%d\n", sizes[0], sizes[1]); fflush(stderr);
    2717     write(0, &sizes, sizeof(sizes));
    2718     write(0, offsets, offsets_size*sizeof(offsets[0]));
    2719     write(0, rle, rle_size);    //unsigned byte
    2720 #else
    2721     NanoVis::ppm_write("nv>image -bytes");
    2722 #endif
    2723 }
    2724 
     2649    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, screen_buffer);
     2650    //glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, screen_buffer); // INSOO's
     2651*/
     2652
     2653
     2654    // HELP ME
     2655    // GEORGE
     2656    // I am not sure what I should do
     2657    //char prefix[200];
     2658    //sprintf(prefix, "nv>height_top_view %s %g %g", volArg, min, max);
     2659    //ppm_write(prefix);
     2660    //write(0, "\n", 1);
     2661   
     2662
     2663
     2664
     2665    //plane_render->remove_plane(index);
     2666   
     2667    resize_offscreen_buffer(old_width, old_height);
     2668
     2669    return TCL_OK;
     2670}
     2671
  • trunk/packages/vizservers/nanovis/Grid.cpp

    r1111 r1161  
    141141            glLoadIdentity();
    142142            glTranslatef((int) wx, viewport[3] - (int) wy, 0);
    143             _font->draw(xAxis.name());
     143            const char *name = xAxis.name();
     144            if (name == NULL) {
     145                name = "???";
     146            }
     147            _font->draw(name);
    144148        }
    145149       
     
    147151            glLoadIdentity();
    148152            glTranslatef((int) wx, viewport[3] - (int)wy, 0);
    149             _font->draw(yAxis.name());
     153            const char *name = yAxis.name();
     154            if (name == NULL) {
     155                name = "???";
     156            }
     157            _font->draw(name);
    150158        }
    151159       
     
    153161            glLoadIdentity();
    154162            glTranslatef((int) wx, (int) viewport[3] - (int)wy, 0.0f);
    155             _font->draw(zAxis.name());
     163            const char *name = zAxis.name();
     164            if (name == NULL) {
     165                name = "???";
     166            }
     167            _font->draw(name);
    156168        }
    157169       
  • trunk/packages/vizservers/nanovis/HeightMap.cpp

    r1112 r1161  
    6565        glCullFace((GLuint) renderContext->getCullMode());
    6666    }
    67 
    6867    glPolygonMode(GL_FRONT_AND_BACK, (GLuint) renderContext->getPolygonMode());
    6968    glShadeModel((GLuint) renderContext->getShadingModel());
     
    337336        }
    338337    }
     338#ifdef notdef
     339    if (retainScale_) {
     340        // Check the units of each axis.  If they are the same, we want to
     341        // retain the surface's aspect ratio when transforming coordinates to
     342        // the grid. Use the range of the longest axis when the units are the
     343        // same. 
     344        if (xAxis.units() != NULL) && (xAxis.units() == yAxis.units()) {
     345        }
     346        if (yAxis.units() != NULL) && (yAxis.units() == zAxis.units()) {
     347        }
     348    }
     349#endif
     350
    339351    wAxis.SetRange(min, max);
    340352    yAxis.SetRange(min, max);
     
    444456
    445457    // The range of the grid's y-axis 0..1 represents the distance between the
    446     // smallest and largest major ticks for all surfaces plotted.  Translate 
     458    // smallest and largest major ticks for all surfaces plotted.  Translate
    447459    // this surface's y-values (heights) into the grid's axis coordinates.
    448460
     
    459471    }
    460472
    461     // Normalize the mesh coordinates (x and z min/max) the range of
    462     // the major ticks for the x and z grid axes as well.
     473    // Normalize the mesh coordinates (x and z min/max) the range of the major
     474    // ticks for the x and z grid axes as well.
    463475
    464476    float xScale, zScale;
     
    499511    delete [] normHeights;
    500512}
     513
  • trunk/packages/vizservers/nanovis/nanovis.cpp

    r1156 r1161  
    1616 */
    1717
     18#include <fcntl.h>
     19#include <fstream>
    1820#include <getopt.h>
     21#include <iostream>
     22#include <math.h>
     23#include <memory.h>
     24#include <signal.h>
     25#include <sstream>
    1926#include <stdio.h>
    20 #include <math.h>
    21 #include <fstream>
    22 #include <iostream>
    23 #include <sstream>
     27#include <stdlib.h>
    2428#include <string>
    25 #include <memory.h>
     29#include <sys/resource.h>
     30#include <sys/time.h>
     31#include <sys/times.h>
     32#include <sys/types.h>
    2633#include <time.h>
    27 #include <sys/time.h>
    28 #include <sys/types.h>
    2934#include <unistd.h>
    30 #include <fcntl.h>
    31 #include <signal.h>
    32 #include <stdlib.h>
    3335
    3436#include "Nv.h"
     
    7981    X_POS = 1, Y_POS = 2, Z_POS = 3, X_NEG = -1, Y_NEG = -2, Z_NEG = -3
    8082};
     83
     84#define STATSFILE       "/var/tmp/visservers/data.xml"
     85#define KEEPSTATS       1
     86
     87#define CVT2SECS(x)  ((double)(x).tv_sec) + ((double)(x).tv_usec * 1.0e-6)
     88
     89#define TRUE    1
     90#define FALSE   0
     91
     92typedef struct {
     93    pid_t pid;
     94    size_t nFrames;             /* # of frames sent to client. */
     95    size_t nBytes;              /* # of bytes for all frames. */
     96    size_t nCommands;           /* # of commands executed */
     97    double cmdTime;             /* Elasped time spend executing commands. */
     98    struct timeval start;       /* Start of elapsed time. */
     99} Stats;
     100
     101static Stats stats;
    81102
    82103// STATIC MEMBER DATA
     
    109130bool NanoVis::config_pending = false;
    110131
     132Tcl_Interp *NanoVis::interp;
     133Tcl_DString NanoVis::cmdbuffer;
    111134
    112135//frame buffer for final rendering
     
    143166
    144167// in Command.cpp
    145 extern void xinetd_listen();
    146 extern void initTcl();
     168extern Tcl_Interp *initTcl();
    147169
    148170//ParticleSystem* psys;
     
    262284}
    263285
     286void removeAllData()
     287{
     288    //
     289}
     290
     291
     292#ifdef KEEPSTATS
     293
     294static int
     295WriteStats(const char *who, int code)
     296{
     297    double start, finish;
     298    pid_t pid;
     299    char buf[BUFSIZ];
     300    Tcl_DString ds;
     301
     302    {
     303        struct timeval tv;
     304
     305        /* Get ending time.  */
     306        gettimeofday(&tv, NULL);
     307        finish = CVT2SECS(tv);
     308        tv = stats.start;
     309        start = CVT2SECS(tv);
     310    }
     311    /*
     312     * Session information:
     313     *   1. Start date of session in seconds.
     314     *   2. Process ID
     315     *   3. Number of frames returned.
     316     *   4. Number of bytes total returned (in frames).
     317     *   5. Total elapsed time of all commands.
     318     *   6. Total elapsed time of session.
     319     *   7. Exit code of pymol server.
     320     *   8. User time. 
     321     *   9. System time.
     322     *  10. Maximum resident size.
     323     */
     324    pid = getpid();
     325    Tcl_DStringInit(&ds);
     326   
     327    sprintf(buf, "<session server=\"%s\" ", who);
     328    Tcl_DStringAppend(&ds, buf, -1);
     329
     330    strcpy(buf, ctime(&stats.start.tv_sec));
     331
     332    buf[strlen(buf) - 1] = '\0';
     333    Tcl_DStringAppend(&ds, "date=\"", -1);
     334    Tcl_DStringAppend(&ds, buf, -1);
     335    Tcl_DStringAppend(&ds, "\" ", -1);
     336
     337    sprintf(buf, "date_secs=\"%ld\" ", stats.start.tv_sec);
     338    Tcl_DStringAppend(&ds, buf, -1);
     339
     340    sprintf(buf, "pid=\"%d\" ", pid);
     341    Tcl_DStringAppend(&ds, buf, -1);
     342    sprintf(buf, "num_frames=\"%lu\" ", (unsigned long int)stats.nFrames);
     343    Tcl_DStringAppend(&ds, buf, -1);
     344    sprintf(buf, "frame_bytes=\"%lu\" ", (unsigned long int)stats.nBytes);
     345    Tcl_DStringAppend(&ds, buf, -1);
     346    sprintf(buf, "num_commands=\"%lu\" ", (unsigned long int)stats.nCommands);
     347    Tcl_DStringAppend(&ds, buf, -1);
     348    sprintf(buf, "cmd_time=\"%g\" ", stats.cmdTime);
     349    Tcl_DStringAppend(&ds, buf, -1);
     350    sprintf(buf, "session_time=\"%g\" ", finish - start);
     351    Tcl_DStringAppend(&ds, buf, -1);
     352    sprintf(buf, "status=\"%d\" ", code);
     353    Tcl_DStringAppend(&ds, buf, -1);
     354    {
     355        long clocksPerSec = sysconf(_SC_CLK_TCK);
     356        double clockRes = 1.0 / clocksPerSec;
     357        struct tms tms;
     358
     359        memset(&tms, 0, sizeof(tms));
     360        if (times(&tms) < 0) {
     361            fprintf(NanoVis::logfile, "can't get times: %s\n", strerror(errno));
     362        }
     363        sprintf(buf, "utime=\"%g\" ", tms.tms_utime * clockRes);
     364        Tcl_DStringAppend(&ds, buf, -1);
     365        sprintf(buf, "stime=\"%g\" ", tms.tms_stime * clockRes);
     366        Tcl_DStringAppend(&ds, buf, -1);
     367        sprintf(buf, "cutime=\"%g\" ", tms.tms_cutime * clockRes);
     368        Tcl_DStringAppend(&ds, buf, -1);
     369        sprintf(buf, "cstime=\"%g\" ", tms.tms_cstime * clockRes);
     370        Tcl_DStringAppend(&ds, buf, -1);
     371    }
     372    Tcl_DStringAppend(&ds, "/>\n", -1);
     373
     374    {
     375        int f;
     376        ssize_t length;
     377        int result;
     378
     379        length = Tcl_DStringLength(&ds);
     380        f = open(STATSFILE, O_APPEND | O_CREAT | O_WRONLY, 0600);
     381        result = FALSE;
     382        if (f < 0) {
     383            goto error;
     384        }
     385        if (write(f, Tcl_DStringValue(&ds), length) != length) {
     386            goto error;
     387        }
     388        result = TRUE;
     389 error:
     390        if (f >= 0) {
     391            close(f);
     392        }
     393        Tcl_DStringFree(&ds);
     394        return result;
     395    }
     396}
     397#endif
     398
     399static void
     400DoExit(int code)
     401{
     402    removeAllData();
     403    NvExit();
     404#ifdef KEEPSTATS
     405    WriteStats("nanovis", code);
     406#endif
     407    exit(code);
     408}
     409
    264410//report errors related to CG shaders
    265411void
     
    275421        Trace("Cg error, exiting...\n");
    276422        cgDestroyContext(g_context);
    277         exit(-1);
     423        DoExit(-1);
    278424    }
    279425}
     
    295441    cgGLLoadProgram(program);
    296442    return program;
     443}
     444
     445static int
     446ExecuteCommand(Tcl_Interp *interp, Tcl_DString *dsPtr)
     447{
     448    struct timeval tv;
     449    double start, finish;
     450    int result;
     451
     452    gettimeofday(&tv, NULL);
     453    start = CVT2SECS(tv);
     454
     455    if (debug_flag) {
     456        fprintf(NanoVis::logfile, "%s\n", Tcl_DStringValue(dsPtr));
     457    }
     458    result = Tcl_Eval(interp, Tcl_DStringValue(dsPtr));
     459    Tcl_DStringSetLength(dsPtr, 0);
     460
     461    gettimeofday(&tv, NULL);
     462    finish = CVT2SECS(tv);
     463
     464    stats.cmdTime += finish - start;
     465    stats.nCommands++;
     466    return result;
    297467}
    298468
     
    632802        cgDestroyContext(g_context);
    633803        fflush(stdout);
    634         exit(-1);
     804        DoExit(-1);
    635805    }
    636806}
     
    646816        fprintf(stderr, "no path defined for shaders or resources\n");
    647817        fflush(stderr);
    648         exit(1);
     818        DoExit(-1);
    649819    }
    650820    // init GLEW
     
    661831        fprintf(stderr, "can't set file path to %s\n", path);
    662832        fflush(stderr);
    663         exit(1);
     833        DoExit(1);
    664834    }
    665835
     
    9131083        }
    9141084    }
    915     fwrite((void*) header, SIZEOF_BMP_HEADER, 1, f);
    916     fwrite((void*) screen_buffer, (3*win_width+pad)*win_height, 1, f);
     1085    fwrite(header, SIZEOF_BMP_HEADER, 1, f);
     1086    fwrite(screen_buffer, (3*win_width+pad)*win_height, 1, f);
    9171087    fclose(f);
    9181088}
     
    9901160    write(0, header, SIZEOF_BMP_HEADER);
    9911161    write(0, screen_buffer, (3*win_width+pad)*win_height);
     1162    stats.nFrames++;
     1163    stats.nBytes += (3*win_width+pad)*win_height;
    9921164}
    9931165
     
    10501222    writev(0, iov, nRecs);
    10511223    free(iov);
     1224    stats.nFrames++;
     1225    stats.nBytes += (bytesPerRow * win_height);
    10521226}
    10531227
     
    10841258
    10851259#ifdef notdef
    1086       struct timespec ts;
    1087       ts.tv_sec = 0;
    1088       ts.tv_nsec = 300000000;
    1089       nanosleep(&ts, 0);
     1260    struct timespec ts;
     1261    ts.tv_sec = 0;
     1262    ts.tv_nsec = 300000000;
     1263    nanosleep(&ts, 0);
    10901264#endif
    10911265#ifdef XINETD
    1092     xinetd_listen();
     1266    NanoVis::xinetd_listen();
    10931267#else
    10941268    glutPostRedisplay();
     
    17441918   switch (key) {
    17451919   case 'q':
    1746        exit(0);
     1920       DoExit(0);
    17471921       break;
    17481922   case '+':
     
    19202094#endif
    19212095
    1922 void removeAllData()
    1923 {
     2096
     2097void
     2098NanoVis::xinetd_listen(void)
     2099{
     2100    int flags = fcntl(0, F_GETFL, 0);
     2101    fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
     2102
     2103    int status = TCL_OK;
     2104    int npass = 0;
     2105
    19242106    //
     2107    //  Read and execute as many commands as we can from stdin...
     2108    //
     2109    while (status == TCL_OK) {
     2110        //
     2111        //  Read the next command from the buffer.  First time through we
     2112        //  block here and wait if necessary until a command comes in.
     2113        //
     2114        //  BE CAREFUL: Read only one command, up to a newline.  The "volume
     2115        //  data follows" command needs to be able to read the data
     2116        //  immediately following the command, and we shouldn't consume it
     2117        //  here.
     2118        //
     2119        while (1) {
     2120            int c = fgetc(NanoVis::stdin);
     2121            char ch;
     2122            if (c <= 0) {
     2123                if (npass == 0) {
     2124                    DoExit(0);
     2125                } else {
     2126                    break;
     2127                }
     2128            }
     2129            ch = (char)c;
     2130            Tcl_DStringAppend(&cmdbuffer, &ch, 1);
     2131
     2132            if (ch=='\n' && Tcl_CommandComplete(Tcl_DStringValue(&cmdbuffer))) {
     2133                break;
     2134            }
     2135        }
     2136        // no command? then we're done for now
     2137        if (Tcl_DStringLength(&cmdbuffer) == 0) {
     2138            break;
     2139        }
     2140
     2141        // back to original flags during command evaluation...
     2142        fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
     2143        status = ExecuteCommand(interp, &cmdbuffer);
     2144        // non-blocking for next read -- we might not get anything
     2145        fcntl(0, F_SETFL, flags | O_NONBLOCK);
     2146        npass++;
     2147    }
     2148    fcntl(0, F_SETFL, flags);
     2149
     2150    if (status != TCL_OK) {
     2151        const char *string;
     2152        int nBytes;
     2153
     2154        string = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), &nBytes);
     2155        struct iovec iov[3];
     2156        iov[0].iov_base = (char *)"NanoVis Server Error: ";
     2157        iov[0].iov_len = strlen((char *)iov[0].iov_base);
     2158        iov[1].iov_base = (char *)string;
     2159        iov[1].iov_len = nBytes;
     2160        iov[2].iov_base = (char *)'\n';
     2161        iov[2].iov_len = 1;
     2162        writev(0, iov, 3);
     2163        return;
     2164    }
     2165
     2166    //
     2167    // This is now in "FlowCmd()":
     2168    //  Generate the latest frame and send it back to the client
     2169    //
     2170    /*
     2171      if (NanoVis::licRenderer && NanoVis::licRenderer->isActivated())
     2172      {
     2173      NanoVis::licRenderer->convolve();
     2174      }
     2175
     2176      if (NanoVis::particleRenderer && NanoVis::particleRenderer->isActivated())
     2177      {
     2178      NanoVis::particleRenderer->advect();
     2179      }
     2180    */
     2181
     2182    NanoVis::update();
     2183
     2184    NanoVis::offscreen_buffer_capture();  //enable offscreen render
     2185
     2186    NanoVis::display();
     2187
     2188    // INSOO
     2189#ifdef XINETD
     2190    NanoVis::read_screen();
     2191    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
     2192#else
     2193    NanoVis::display_offscreen_buffer(); //display the final rendering on screen
     2194    NanoVis::read_screen();
     2195    glutSwapBuffers();
     2196#endif
     2197
     2198#if DO_RLE
     2199    do_rle();
     2200    int sizes[2] = {  offsets_size*sizeof(offsets[0]), rle_size };
     2201    fprintf(stderr, "Writing %d,%d\n", sizes[0], sizes[1]); fflush(stderr);
     2202    write(0, &sizes, sizeof(sizes));
     2203    write(0, offsets, offsets_size*sizeof(offsets[0]));
     2204    write(0, rle, rle_size);    //unsigned byte
     2205#else
     2206    NanoVis::ppm_write("nv>image -bytes");
     2207#endif
    19252208}
    19262209
     
    19322215    const char *path;
    19332216    char *newPath;
     2217    struct timeval tv;
    19342218
    19352219    newPath = NULL;
     
    19372221    NanoVis::stdin = stdin;
    19382222    NanoVis::logfile = stderr;
    1939     opterr = 1;
     2223
     2224    gettimeofday(&tv, NULL);
     2225    stats.start = tv;
     2226
    19402227    while (1) {
    19412228        static struct option long_options[] = {
     
    20472334    }
    20482335    NanoVis::initGL();
    2049     initTcl();
     2336    Tcl_DStringInit(&NanoVis::cmdbuffer);
     2337    NanoVis::interp = initTcl();
    20502338
    20512339#ifdef EVENTLOG
     
    20552343    glutMainLoop();
    20562344
    2057     removeAllData();
    2058 
    2059     NvExit();
    2060     return 0;
     2345    DoExit(0);
    20612346}
    20622347
  • trunk/packages/vizservers/nanovis/nanovis.h

    r1156 r1161  
    141141    static bool vector_on;
    142142
     143    static Tcl_Interp *interp;
     144    static Tcl_DString cmdbuffer;
    143145
    144146    static TransferFunction* get_transfunc(const char *name);
     
    164166    static Volume *load_volume(int index, int width, int height, int depth,
    165167        int n, float* data, double vmin, double vmax, double nzero_min);
     168    static void xinetd_listen(void);
    166169    static int render_2d_contour(HeightMap* heightmap, int width, int height);
    167170
  • trunk/packages/vizservers/pymolproxy/pymolproxy.c

    r1155 r1161  
    2020
    2121#include <stdio.h>
     22#include <time.h>
    2223#include <assert.h>
    2324#include <unistd.h>
     
    3132#include <poll.h>
    3233#include <sys/time.h>
     34#include <sys/times.h>
    3335#include <fcntl.h>
    3436#include <netinet/in.h>
     
    4648#endif
    4749
     50#define FALSE 0
     51#define TRUE  1
     52
    4853#define IO_TIMEOUT (30000)
    49 #define STATSFILE       "/var/tmp/pymolproxy.csv"
     54#define STATSFILE       "/var/tmp/visservers/data.xml"
     55#define KEEPSTATS       1
     56#define CVT2SECS(x)  ((double)(x).tv_sec) + ((double)(x).tv_usec * 1.0e-6)
    5057
    5158typedef struct {
    52     unsigned int date;
     59    pid_t child;                /* Child process. */
    5360    size_t nFrames;             /* # of frames sent to client. */
    5461    size_t nBytes;              /* # of bytes for all frames. */
     
    6370static int debug = 1;
    6471
    65 static void
    66 trace TCL_VARARGS_DEF(char *, arg1)
    67 {
    68     if (debug) {
    69         char *format;
    70         va_list args;
    71 
    72         format = TCL_VARARGS_START(char *, arg1, args);
    73         vfprintf(flog, format, args);
    74         fprintf(flog, "\n");
    75         fflush(flog);
    76     }
    77 }
    7872
    7973typedef struct {
     
    10397} PymolProxy;
    10498
    105 static int
    106 ExecuteCommand(Tcl_Interp *interp, Tcl_DString *dsPtr, Stats *statsPtr)
     99static void
     100trace TCL_VARARGS_DEF(char *, arg1)
     101{
     102    if (debug) {
     103        char *format;
     104        va_list args;
     105
     106        format = TCL_VARARGS_START(char *, arg1, args);
     107        vfprintf(flog, format, args);
     108        fprintf(flog, "\n");
     109        fflush(flog);
     110    }
     111}
     112
     113#ifdef KEEPSTATS
     114
     115static int
     116WriteStats(const char *who, int code)
     117{
     118    double start, finish;
     119    pid_t pid;
     120    char buf[BUFSIZ];
     121    Tcl_DString ds;
     122
     123    {
     124        struct timeval tv;
     125
     126        /* Get ending time.  */
     127        gettimeofday(&tv, NULL);
     128        finish = CVT2SECS(tv);
     129        tv = stats.start;
     130        start = CVT2SECS(tv);
     131    }
     132    /*
     133     * Session information:
     134     *   1. Start date of session in seconds.
     135     *   2. Process ID
     136     *   3. Number of frames returned.
     137     *   4. Number of bytes total returned (in frames).
     138     *   5. Total elapsed time of all commands.
     139     *   6. Total elapsed time of session.
     140     *   7. Exit code of pymol server.
     141     *   8. User time. 
     142     *   9. System time.
     143     *  10. Maximum resident size.
     144     */
     145    pid = getpid();
     146    Tcl_DStringInit(&ds);
     147
     148    sprintf(buf, "<session server=\"%s\" ", who);
     149    Tcl_DStringAppend(&ds, buf, -1);
     150
     151    strcpy(buf, ctime(&stats.start.tv_sec));
     152
     153    buf[strlen(buf) - 1] = '\0';
     154    Tcl_DStringAppend(&ds, "date=\"", -1);
     155    Tcl_DStringAppend(&ds, buf, -1);
     156    Tcl_DStringAppend(&ds, "\" ", -1);
     157
     158    sprintf(buf, "date_secs=\"%ld\" ", stats.start.tv_sec);
     159    Tcl_DStringAppend(&ds, buf, -1);
     160
     161    sprintf(buf, "pid=\"%d\" ", pid);
     162    Tcl_DStringAppend(&ds, buf, -1);
     163    sprintf(buf, "num_frames=\"%lu\" ", (unsigned long int)stats.nFrames);
     164    Tcl_DStringAppend(&ds, buf, -1);
     165    sprintf(buf, "frame_bytes=\"%lu\" ", (unsigned long int)stats.nBytes);
     166    Tcl_DStringAppend(&ds, buf, -1);
     167    sprintf(buf, "num_commands=\"%lu\" ", (unsigned long int)stats.nCommands);
     168    Tcl_DStringAppend(&ds, buf, -1);
     169    sprintf(buf, "cmd_time=\"%g\" ", stats.cmdTime);
     170    Tcl_DStringAppend(&ds, buf, -1);
     171    sprintf(buf, "session_time=\"%g\" ", finish - start);
     172    Tcl_DStringAppend(&ds, buf, -1);
     173    sprintf(buf, "status=\"%d\" ", code);
     174    Tcl_DStringAppend(&ds, buf, -1);
     175    {
     176        long clocksPerSec = sysconf(_SC_CLK_TCK);
     177        double clockRes = 1.0 / clocksPerSec;
     178        struct tms tms;
     179
     180        memset(&tms, 0, sizeof(tms));
     181        if (times(&tms) < 0) {
     182            fprintf(flog, "can't get times: %s\n", strerror(errno));
     183        }
     184        sprintf(buf, "utime=\"%g\" ", tms.tms_utime * clockRes);
     185        Tcl_DStringAppend(&ds, buf, -1);
     186        sprintf(buf, "stime=\"%g\" ", tms.tms_stime * clockRes);
     187        Tcl_DStringAppend(&ds, buf, -1);
     188        sprintf(buf, "cutime=\"%g\" ", tms.tms_cutime * clockRes);
     189        Tcl_DStringAppend(&ds, buf, -1);
     190        sprintf(buf, "cstime=\"%g\" ", tms.tms_cstime * clockRes);
     191        Tcl_DStringAppend(&ds, buf, -1);
     192    }
     193    Tcl_DStringAppend(&ds, "/>\n", -1);
     194
     195    {
     196        int f;
     197        ssize_t length;
     198        int result;
     199
     200        length = Tcl_DStringLength(&ds);
     201        f = open(STATSFILE, O_APPEND | O_CREAT | O_WRONLY, 0600);
     202        result = FALSE;
     203        if (f < 0) {
     204            goto error;
     205        }
     206        if (write(f, Tcl_DStringValue(&ds), length) != length) {
     207            goto error;
     208        }
     209        result = TRUE;
     210 error:
     211        if (f >= 0) {
     212            close(f);
     213        }
     214        Tcl_DStringFree(&ds);
     215        return result;
     216    }
     217}
     218#endif
     219
     220static void
     221DoExit(int code)
     222{
     223    char fileName[200];
     224#ifdef KEEPSTATS
     225    WriteStats("pymolproxy", code);
     226#endif
     227    sprintf(fileName, "/tmp/pymol%d.pdb", getpid());
     228    unlink(fileName);
     229    exit(code);
     230}
     231
     232static int
     233ExecuteCommand(Tcl_Interp *interp, Tcl_DString *dsPtr)
    107234{
    108235    struct timeval tv;
     
    111238
    112239    gettimeofday(&tv, NULL);
    113     start = ((double)tv.tv_sec) + ((double)tv.tv_usec * 1.0e-6);
     240    start = CVT2SECS(tv);
     241
    114242    result = Tcl_Eval(interp, Tcl_DStringValue(dsPtr));
    115243    trace("Executed (%s)", Tcl_DStringValue(dsPtr));
    116244    Tcl_DStringSetLength(dsPtr, 0);
     245
    117246    gettimeofday(&tv, NULL);
    118    
    119     finish = ((double)tv.tv_sec) + ((double)tv.tv_usec * 1.0e-6);
    120     statsPtr->cmdTime += finish - start;
    121     statsPtr->nCommands++;
     247    finish = CVT2SECS(tv);
     248
     249    stats.cmdTime += finish - start;
     250    stats.nCommands++;
    122251    return result;
    123252}
    124253
    125 #ifdef KEEPSTATS
    126 
    127 static int
    128 WriteStats(Stats *statsPtr, int code)
    129 {
    130     struct timeval tv;
    131     double start, finish;
    132     int f;
    133     size_t length;
    134     char buf[4000];
    135 
    136     /* Get ending time.  */
    137     gettimeofday(&tv, NULL);
    138     finish = ((double)tv.tv_sec) + ((double)tv.tv_usec * 1.0e-6);
    139     tv = statsPtr->start;
    140     start = ((double)tv.tv_sec) + ((double)tv.tv_usec * 1.0e-6);
    141     sprintf(buf, "\"pymolproxy\",%d,%d,%d,%d,%d,%d,%g,%g",
    142             getpid(),
    143             code,
    144             statsPtr->date,
    145             statsPtr->nFrames,
    146             statsPtr->nBytes,
    147             statsPtr->nCommands,
    148             statsPtr->cmdTime,
    149             finish - start);
    150     f = open(STATSFILE, O_APPEND | O_CREAT | O_WRONLY, 0600);
    151     if (f < 0) {
    152         return 0;
    153     }
    154     length = strlen(buf);
    155     if (write(f, buf, length) != length) {
    156         return 0;
    157     }
    158     close(f);
    159     return 1;
    160 }
    161 #endif
    162254
    163255INLINE static void
     
    12471339        execvp(argv[0], argv);
    12481340        trace("pymolproxy: Failed to start pyMol %s\n", argv[0]);
    1249         exit(-1);
    1250     }
    1251        
     1341        exit(-1);
     1342    }
     1343    stats.child = pid;
     1344
    12521345    /* close opposite end of pipe, these now belong to the child process        */
    12531346    close(pairIn[0]);
     
    13581451                        int result;
    13591452
    1360                         result = ExecuteCommand(interp, &cmdbuffer, &stats);
     1453                        result = ExecuteCommand(interp, &cmdbuffer);
    13611454                        if (timeout == 0) status = 0; // send update
    13621455                    }
     
    14201513   
    14211514    status = waitpid(pid, &result, WNOHANG);
    1422 #ifdef KEEPSTATS
    1423     WriteStats(&stats, status);
    1424 #endif
    14251515    if (status == -1) {
    14261516        trace("pymolproxy: error waiting on pymol server to exit (%d)\n", errno);
     
    14461536   
    14471537    Tcl_DeleteInterp(interp);
    1448    
    1449     return( (status == pid) ? 0 : 1);
     1538    DoExit(result);
     1539    return 0;
    14501540}
    14511541
Note: See TracChangeset for help on using the changeset viewer.