Ignore:
Timestamp:
Feb 24, 2013 1:11:18 PM (11 years ago)
Author:
gah
Message:

merge (by hand) with Rappture1.2 branch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/pymolproxy/pymolproxy2.c

    r3177 r3330  
    113113
    114114#define IO_TIMEOUT (30000)
    115 #define KEEPSTATS       1
     115#define STATSDIR        "/var/tmp/visservers"
     116#define STATSFILE       STATSDIR "/" "pymol_log.tcl"
     117#define LOCKFILE        STATSDIR "/" "LCK..pymol"
    116118#define CVT2SECS(x)  ((double)(x).tv_sec) + ((double)(x).tv_usec * 1.0e-6)
    117119
    118120typedef struct {
    119121    pid_t pid;                          /* Child process. */
    120     size_t nFrames;                     /* # of frames sent to client. */
    121     size_t nBytes;                      /* # of bytes for all frames. */
    122     size_t nCommands;                   /* # of commands executed */
     122    size_t numFrames;                   /* # of frames sent to client. */
     123    size_t numBytes;                    /* # of bytes for all frames. */
     124    size_t numCommands;                 /* # of commands executed */
    123125    double cmdTime;                     /* Elapsed time spend executing
    124126                                         * commands. */
     
    138140                                         * the pymol server to the least. */
    139141    int id;
    140     ssize_t nWritten;                   /* Number of bytes of image data
     142    ssize_t numWritten;                 /* Number of bytes of image data
    141143                                         * already delivered.*/
    142144    size_t bytesLeft;                   /* Number of bytes of image data left
     
    270272
    271273static int
    272 SendCmd(PymolProxy *proxyPtr, const char *format, ...)
     274SendToPymol(PymolProxy *proxyPtr, const char *format, ...)
    273275{
    274276    va_list ap;
    275277    char buffer[BUFSIZ];
    276278    int result;
    277     ssize_t nWritten;
     279    ssize_t numWritten;
    278280    size_t length;
    279281
     
    295297    /* Write the command out to the server. */
    296298    length = strlen(buffer);
    297     nWritten = write(proxyPtr->sin, buffer, length);
    298     if (nWritten != length) {
     299    numWritten = write(proxyPtr->sin, buffer, length);
     300    if (numWritten != length) {
    299301        ERROR("short write to pymol (wrote=%d, should have been %d): %s",
    300               nWritten, length, strerror(errno));
    301     }
     302              numWritten, length, strerror(errno));
     303    }
     304    proxyPtr->status = result;
    302305    return  proxyPtr->status;
    303306}
     
    543546}
    544547
     548static int
     549GetFileLock()
     550{
     551    int numTries;
     552
     553    for (numTries = 0; numTries < 10; numTries++) {
     554        int f;
     555
     556        f = open(LOCKFILE, O_TRUNC | O_CREAT | O_EXCL | O_WRONLY, 0600);
     557        if (f >= 0) {
     558            char buf[200];
     559            ssize_t numWritten;
     560            size_t numBytes;
     561
     562            sprintf(buf, "%d\n", getpid());
     563            numBytes = strlen(buf);
     564            numWritten = write(f, buf, numBytes);
     565            if (numWritten != (ssize_t)numBytes) {
     566                ERROR("Wrote short lock file");
     567            }
     568            close(f);
     569            return 0;
     570        }
     571        sleep(1);                       /* Wait for lock to release. */
     572    }
     573    ERROR("Failed to open lock file");
     574    return -1;
     575}
     576
     577static void
     578ReleaseFileLock()
     579{
     580    unlink(LOCKFILE);
     581}
     582
     583static int
     584WriteToStatsFile(const char *s, size_t length)
     585{
     586    int f;
     587
     588    if (access(STATSDIR, X_OK) != 0) {
     589        mkdir(STATSDIR, 0770);
     590    }
     591    if (GetFileLock() < 0) {
     592        return -1;
     593    }
     594    f = open(STATSFILE, O_APPEND | O_CREAT | O_WRONLY, 0600);
     595    ReleaseFileLock();
     596    if (f < 0) {
     597        return -1;
     598    }
     599    if (write(f, s, length) != (ssize_t)length) {
     600        close(f);
     601        return -1;
     602    }
     603    close(f);
     604    return 0;
     605}
     606
     607static int
     608ServerStats(int code)
     609{
     610    double start, finish;
     611    char buf[BUFSIZ];
     612    Tcl_DString ds;
     613    int result;
     614
     615    {
     616        struct timeval tv;
     617
     618        /* Get ending time.  */
     619        gettimeofday(&tv, NULL);
     620        finish = CVT2SECS(tv);
     621        tv = stats.start;
     622        start = CVT2SECS(tv);
     623    }
     624    /*
     625     * Session information:
     626     *   - Name of render server
     627     *   - Process ID
     628     *   - Hostname where server is running
     629     *   - Start date of session
     630     *   - Start date of session in seconds
     631     *   - Number of frames returned
     632     *   - Number of bytes total returned (in frames)
     633     *   - Number of commands received
     634     *   - Total elapsed time of all commands
     635     *   - Total elapsed time of session
     636     *   - Exit code of vizserver
     637     *   - User time
     638     *   - System time
     639     *   - User time of children
     640     *   - System time of children
     641     */
     642
     643    Tcl_DStringInit(&ds);
     644   
     645    Tcl_DStringAppendElement(&ds, "render_stop");
     646    /* renderer */
     647    Tcl_DStringAppendElement(&ds, "renderer");
     648    Tcl_DStringAppendElement(&ds, "pymol");
     649    /* pid */
     650    Tcl_DStringAppendElement(&ds, "pid");
     651    sprintf(buf, "%d", getpid());
     652    Tcl_DStringAppendElement(&ds, buf);
     653    /* host */
     654    Tcl_DStringAppendElement(&ds, "host");
     655    gethostname(buf, BUFSIZ-1);
     656    buf[BUFSIZ-1] = '\0';
     657    Tcl_DStringAppendElement(&ds, buf);
     658    /* date */
     659    Tcl_DStringAppendElement(&ds, "date");
     660    strcpy(buf, ctime(&stats.start.tv_sec));
     661    buf[strlen(buf) - 1] = '\0';
     662    Tcl_DStringAppendElement(&ds, buf);
     663    /* date_secs */
     664    Tcl_DStringAppendElement(&ds, "date_secs");
     665    sprintf(buf, "%ld", stats.start.tv_sec);
     666    Tcl_DStringAppendElement(&ds, buf);
     667    /* num_frames */
     668    Tcl_DStringAppendElement(&ds, "num_frames");
     669    sprintf(buf, "%lu", (unsigned long int)stats.numFrames);
     670    Tcl_DStringAppendElement(&ds, buf);
     671    /* frame_bytes */
     672    Tcl_DStringAppendElement(&ds, "frame_bytes");
     673    sprintf(buf, "%lu", (unsigned long int)stats.numBytes);
     674    Tcl_DStringAppendElement(&ds, buf);
     675    /* num_commands */
     676    Tcl_DStringAppendElement(&ds, "num_commands");
     677    sprintf(buf, "%lu", (unsigned long int)stats.numCommands);
     678    Tcl_DStringAppendElement(&ds, buf);
     679    /* cmd_time */
     680    Tcl_DStringAppendElement(&ds, "cmd_time");
     681    sprintf(buf, "%g", stats.cmdTime);
     682    Tcl_DStringAppendElement(&ds, buf);
     683    /* session_time */
     684    Tcl_DStringAppendElement(&ds, "session_time");
     685    sprintf(buf, "%g", finish - start);
     686    Tcl_DStringAppendElement(&ds, buf);
     687    /* status */
     688    Tcl_DStringAppendElement(&ds, "status");
     689    sprintf(buf, "%d", code);
     690    Tcl_DStringAppendElement(&ds, buf);
     691    {
     692        long clocksPerSec = sysconf(_SC_CLK_TCK);
     693        double clockRes = 1.0 / clocksPerSec;
     694        struct tms tms;
     695
     696        memset(&tms, 0, sizeof(tms));
     697        times(&tms);
     698        /* utime */
     699        Tcl_DStringAppendElement(&ds, "utime");
     700        sprintf(buf, "%g", tms.tms_utime * clockRes);
     701        Tcl_DStringAppendElement(&ds, buf);
     702        /* stime */
     703        Tcl_DStringAppendElement(&ds, "stime");
     704        sprintf(buf, "%g", tms.tms_stime * clockRes);
     705        Tcl_DStringAppendElement(&ds, buf);
     706        /* cutime */
     707        Tcl_DStringAppendElement(&ds, "cutime");
     708        sprintf(buf, "%g", tms.tms_cutime * clockRes);
     709        Tcl_DStringAppendElement(&ds, buf);
     710        /* cstime */
     711        Tcl_DStringAppendElement(&ds, "cstime");
     712        sprintf(buf, "%g", tms.tms_cstime * clockRes);
     713        Tcl_DStringAppendElement(&ds, buf);
     714    }
     715    Tcl_DStringAppend(&ds, "\n", -1);
     716    result = WriteToStatsFile(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
     717    Tcl_DStringFree(&ds);
     718    return result;
     719}
     720
    545721
    546722static int
     
    579755    }
    580756    if (bool) {
    581         SendCmd(p, "show cartoon,%s\n", model);
     757        SendToPymol(p, "show cartoon,%s\n", model);
    582758    } else {
    583         SendCmd(p, "hide cartoon,%s\n", model);
     759        SendToPymol(p, "hide cartoon,%s\n", model);
    584760    }
    585761    return p->status;
     
    619795        p->flags |= FORCE_UPDATE;
    620796    }
    621     SendCmd(p, "set cartoon_trace,%d,%s\n", bool, model);
     797    SendToPymol(p, "set cartoon_trace,%d,%s\n", bool, model);
    622798    return p->status;
    623799}
     
    649825        p->flags |= FORCE_UPDATE;
    650826    }
    651     SendCmd(p, "disable %s\n", model);
     827    SendToPymol(p, "disable %s\n", model);
    652828    return p->status;
    653829}
     
    681857        p->flags |= FORCE_UPDATE;
    682858    }
    683     SendCmd(p, "enable %s\n", model);
     859    SendToPymol(p, "enable %s\n", model);
    684860    return p->status;
    685861}
     
    714890    /* Does not invalidate cache? */
    715891
    716     SendCmd(p,"frame %d\n", frame);
     892    SendToPymol(p,"frame %d\n", frame);
    717893    return p->status;
    718894}
    719895
     896/*
     897 * ClientInfoCmd --
     898 *
     899 *      info "hub" value "session" value "date" value name value name value
     900 *       
     901 */
     902static int
     903ClientInfoCmd(ClientData clientData, Tcl_Interp *interp, int argc,
     904              const char *argv[])
     905{
     906    Tcl_DString ds;
     907    int result;
     908    int i;
     909    char buf[BUFSIZ];
     910
     911    Tcl_DStringInit(&ds);
     912    Tcl_DStringAppendElement(&ds, "render_start");
     913    /* renderer */
     914    Tcl_DStringAppendElement(&ds, "renderer");
     915    Tcl_DStringAppendElement(&ds, "nanovis");
     916    /* pid */
     917    Tcl_DStringAppendElement(&ds, "pid");
     918    sprintf(buf, "%d", getpid());
     919    Tcl_DStringAppendElement(&ds, buf);
     920    /* host */
     921    Tcl_DStringAppendElement(&ds, "host");
     922    gethostname(buf, BUFSIZ-1);
     923    buf[BUFSIZ-1] = '\0';
     924    Tcl_DStringAppendElement(&ds, buf);
     925    /* date */
     926    Tcl_DStringAppendElement(&ds, "date");
     927    strcpy(buf, ctime(&stats.start.tv_sec));
     928    buf[strlen(buf) - 1] = '\0';
     929    Tcl_DStringAppendElement(&ds, buf);
     930    /* date_secs */
     931    Tcl_DStringAppendElement(&ds, "date_secs");
     932    sprintf(buf, "%ld", stats.start.tv_sec);
     933    Tcl_DStringAppendElement(&ds, buf);
     934    /* Client arguments. */
     935    for (i = 1; i < argc; i++) {
     936        Tcl_DStringAppendElement(&ds, argv[i]);
     937    }
     938    Tcl_DStringAppend(&ds, "\n", 1);
     939    result = WriteToStatsFile(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
     940    Tcl_DStringFree(&ds);
     941    return result;
     942}
    720943
    721944static int
     
    756979        p->flags |= FORCE_UPDATE;
    757980    }
    758     SendCmd(p, "set label_color,white,%s\nset label_size,%d,%s\n",
     981    SendToPymol(p, "set label_color,white,%s\nset label_size,%d,%s\n",
    759982            model, size, model);
    760983    if (bool) {
    761         SendCmd(p, "label %s,\"%%s%%s\" %% (ID,name)\n", model);
     984        SendToPymol(p, "label %s,\"%%s%%s\" %% (ID,name)\n", model);
    762985    } else {
    763         SendCmd(p, "label %s\n", model);
     986        SendToPymol(p, "label %s\n", model);
    764987    }
    765988    return p->status;
     
    8091032    if (argc < 4) {
    8101033        Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
    811                          " <data>|follows <model> <state> ?<nBytes>?\"",
     1034                         " <data>|follows <model> <state> ?<numBytes>?\"",
    8121035                         (char *)NULL);
    8131036        return TCL_ERROR;
     
    8251048        if (argc != 5) {
    8261049            Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
    827                          " follows <model> <state> <nBytes>\"", (char *)NULL);
     1050                         " follows <model> <state> <numBytes>\"", (char *)NULL);
    8281051            return TCL_ERROR;
    8291052        }
     
    8651088    {
    8661089        int f;
    867         ssize_t nWritten;
     1090        ssize_t numWritten;
    8681091        char fileName[200];
    8691092
     
    8761099            goto error;
    8771100        }
    878         nWritten = write(f, string, numBytes);
    879         if (numBytes != nWritten) {
     1101        numWritten = write(f, string, numBytes);
     1102        if (numBytes != numWritten) {
    8801103            Tcl_AppendResult(interp, "can't write PDB data to \"",
    8811104                fileName, "\": ", Tcl_PosixError(interp), (char *)NULL);
     
    8841107        }
    8851108        close(f);
    886         SendCmd(p, "loadandremovepdbfile %s,%s,%d\n", fileName, name, state);
     1109        SendToPymol(p, "loadandremovepdbfile %s,%s,%d\n", fileName, name,
     1110                    state);
    8871111        p->status = TCL_OK;
    8881112    }
     
    9221146        p->flags |= FORCE_UPDATE;
    9231147    }
    924     SendCmd(p, "set orthoscopic=%d\n", bool);
     1148    SendToPymol(p, "set orthoscopic=%d\n", bool);
    9251149    return p->status;
    9261150}
     
    9851209
    9861210    /* Force pymol to update the current scene. */
    987     SendCmd(p, "refresh\n");
     1211    SendToPymol(p, "refresh\n");
    9881212    /* This is a hack. We're encoding the filename to pass extra information
    9891213     * to the MyPNGWrite routine inside of pymol. Ideally these would be
     
    9931217     * The extra information is contained in the token we get from the
    9941218     * molvisviewer client, the frame number, and rock offset. */
    995     SendCmd(p, "png -:%d:%d:%d\n", p->cacheId, p->frame, p->rockOffset);
     1219    SendToPymol(p, "png -:%d:%d:%d\n", p->cacheId, p->frame, p->rockOffset);
    9961220    return p->status;
    9971221}
     
    10061230
    10071231    /* Force pymol to update the current scene. */
    1008     SendCmd(p, "refresh\n");
     1232    SendToPymol(p, "refresh\n");
    10091233    /* This is a hack. We're encoding the filename to pass extra information
    10101234     * to the MyPNGWrite routine inside of pymol. Ideally these would be
     
    10141238     * The extra information is contained in the token we get from the
    10151239     * molvisviewer client, the frame number, and rock offset. */
    1016     SendCmd(p, "png -:%d:%d:%d,format=1\n", p->cacheId, p->frame,
     1240    SendToPymol(p, "png -:%d:%d:%d,format=1\n", p->cacheId, p->frame,
    10171241            p->rockOffset);
    10181242    p->flags &= ~(UPDATE_PENDING|FORCE_UPDATE);
     
    10461270    /* Force pymol to update the current scene. */
    10471271    if (strcmp(bgcolor, "none") == 0) {
    1048         SendCmd(p, "set ray_opaque_background,off\n");
    1049         SendCmd(p, "refresh\n", bgcolor);
     1272        SendToPymol(p, "set ray_opaque_background,off\n");
     1273        SendToPymol(p, "refresh\n", bgcolor);
    10501274    } else {
    1051         SendCmd(p, "set ray_opaque_background,on\n");
    1052         SendCmd(p, "bg_color %s\nrefresh\n", bgcolor);
     1275        SendToPymol(p, "set ray_opaque_background,on\n");
     1276        SendToPymol(p, "bg_color %s\nrefresh\n", bgcolor);
    10531277    }
    10541278    /* This is a hack. We're encoding the filename to pass extra information
     
    10601284     * molvisviewer client, the frame number, and rock offset.
    10611285     */
    1062     SendCmd(p, "png -:%s:0:0,width=%d,height=%d,ray=1,dpi=300\n",
     1286    SendToPymol(p, "png -:%s:0:0,width=%d,height=%d,ray=1,dpi=300\n",
    10631287            token, width, height);
    1064     SendCmd(p, "bg_color black\n");
     1288    SendToPymol(p, "bg_color black\n");
    10651289    return p->status;
    10661290}
     
    10931317        p->flags |= FORCE_UPDATE;
    10941318    }
    1095     SendCmd(p,"%s\n", cmd);
     1319    SendToPymol(p,"%s\n", cmd);
    10961320    return p->status;
    10971321}
     
    11201344        p->flags |= FORCE_UPDATE;
    11211345    }
    1122     SendCmd(p, "reset\nzoom complete=1\n");
     1346    SendToPymol(p, "reset\nzoom complete=1\n");
    11231347    return p->status;
    11241348}
     
    11521376        p->flags |= FORCE_UPDATE;
    11531377    }
    1154     SendCmd(p,"turn y, %f\n", y - p->rockOffset);
     1378    SendToPymol(p,"turn y, %f\n", y - p->rockOffset);
    11551379    p->rockOffset = y;
    11561380    return p->status;
     
    11971421    }
    11981422    if (strcmp(rep, "ballnstick") == 0) { /* Ball 'n Stick */
    1199         SendCmd(p,
     1423        SendToPymol(p,
    12001424              "set stick_color,white,%s\n"
    12011425              "show sticks,%s\n"
     
    12051429              model, model, model, model, model);
    12061430    } else if (strcmp(rep, "spheres") == 0) { /* spheres */   
    1207         SendCmd(p,
     1431        SendToPymol(p,
    12081432              "hide sticks,%s\n"
    12091433              "show spheres,%s\n"
     
    12141438              model, model, model, model, model, model);
    12151439    } else if (strcmp(rep, "none") == 0) { /* nothing */   
    1216         SendCmd(p,
     1440        SendToPymol(p,
    12171441              "hide sticks,%s\n",
    12181442              "hide spheres,%s\n"
     
    12211445              model, model, model, model);
    12221446    } else if (strcmp(rep, "sticks") == 0) { /* sticks */   
    1223         SendCmd(p,
     1447        SendToPymol(p,
    12241448              "set stick_color,white,%s\n"
    12251449              "show sticks,%s\n"
     
    12291453              model, model, model, model, model);
    12301454    } else if (strcmp(rep, "lines") == 0) { /* lines */   
    1231         SendCmd(p,
     1455        SendToPymol(p,
    12321456              "hide sticks,%s\n"
    12331457              "hide spheres,%s\n"
     
    12361460              model, model, model, model);
    12371461    } else if (strcmp(rep, "cartoon") == 0) { /* cartoon */   
    1238         SendCmd(p,
     1462        SendToPymol(p,
    12391463              "hide sticks,%s\n"
    12401464              "hide spheres,%s\n"
     
    13921616        p->sphereScale = scale;
    13931617    } else {
    1394         SendCmd(p, "set sphere_scale,%f,%s\n", scale, model);
     1618        SendToPymol(p, "set sphere_scale,%f,%s\n", scale, model);
    13951619    }
    13961620    return p->status;
     
    14341658        p->stickRadius = scale;
    14351659    } else {
    1436         SendCmd(p, "set stick_radius,%f,%s\n", scale, model);
     1660        SendToPymol(p, "set stick_radius,%f,%s\n", scale, model);
    14371661    }
    14381662    return p->status;
     
    14731697        p->flags |= FORCE_UPDATE;
    14741698    }
    1475     SendCmd(p,
     1699    SendToPymol(p,
    14761700          "set sphere_transparency,%g,%s\n"
    14771701          "set stick_transparency,%g,%s\n"
     
    15221746        p->flags |= FORCE_UPDATE;
    15231747    }
    1524     SendCmd(p, "vmouse %d,%d,%d,%d,%d\n", arg1, arg2, arg3, arg4, arg5);
     1748    SendToPymol(p, "vmouse %d,%d,%d,%d,%d\n", arg1, arg2, arg3, arg4, arg5);
    15251749    return p->status;
    15261750}
     
    16011825
    16021826    stats.cmdTime += finish - start;
    1603     stats.nCommands++;
     1827    stats.numCommands++;
    16041828    Tcl_DStringSetLength(dsPtr, 0);
    16051829    return result;
     
    16101834{
    16111835    if (p->flags & VIEWPORT_PENDING) {
    1612         SendCmd(p, "viewport %d,%d\n", p->width, p->height);
    1613         SendCmd(p, "refresh\n");
     1836        SendToPymol(p, "viewport %d,%d\n", p->width, p->height);
     1837        SendToPymol(p, "refresh\n");
    16141838        p->flags &= ~VIEWPORT_PENDING;
    16151839    }
     
    16201844{
    16211845    if (p->flags & ZOOM_PENDING) {
    1622         SendCmd(p, "move z,%f\n", p->zoom);
     1846        SendToPymol(p, "move z,%f\n", p->zoom);
    16231847        p->flags &= ~ZOOM_PENDING;
    16241848    }
     
    16291853{
    16301854    if (p->flags & PAN_PENDING) {
    1631         SendCmd(p, "move x,%f\nmove y,%f\n", p->xPan, p->yPan);
     1855        SendToPymol(p, "move x,%f\nmove y,%f\n", p->xPan, p->yPan);
    16321856        p->flags &= ~PAN_PENDING;
    16331857    }
     
    16401864        /* Every pymol command line generates a new rendering. Execute all
    16411865         * three turns as a single command line. */
    1642         SendCmd(p,"turn x,%f\nturn y,%f\nturn z,%f\n", p->xAngle, p->yAngle,
     1866        SendToPymol(p,"turn x,%f\nturn y,%f\nturn z,%f\n", p->xAngle, p->yAngle,
    16431867                p->zAngle);
    16441868        p->xAngle = p->yAngle = p->zAngle = 0.0f;
     
    16511875{
    16521876    if (p->flags & ATOM_SCALE_PENDING) {
    1653         SendCmd(p, "set sphere_scale,%f,all\n", p->sphereScale);
     1877        SendToPymol(p, "set sphere_scale,%f,all\n", p->sphereScale);
    16541878        p->flags &= ~ATOM_SCALE_PENDING;
    16551879    }
     
    16601884{
    16611885    if (p->flags & STICK_RADIUS_PENDING) {
    1662         SendCmd(p, "set stick_radius,%f,all\n", p->stickRadius);
     1886        SendToPymol(p, "set stick_radius,%f,all\n", p->stickRadius);
    16631887        p->flags &= ~STICK_RADIUS_PENDING;
    16641888    }
     
    17151939    }
    17161940    listPtr->headPtr = imgPtr;
    1717     imgPtr->nWritten = 0;
     1941    imgPtr->numWritten = 0;
    17181942    return imgPtr;
    17191943}
     
    17491973#endif
    17501974        for (bytesLeft = imgPtr->bytesLeft; bytesLeft > 0; /*empty*/) {
    1751             ssize_t nWritten;
     1975            ssize_t numWritten;
    17521976#if WRITE_DEBUG
    17531977            DEBUG("image %d: bytesLeft=%d", imgPtr->id, bytesLeft);
    17541978#endif
    1755             nWritten = write(fd, imgPtr->data + imgPtr->nWritten, bytesLeft);
     1979            numWritten = write(fd, imgPtr->data + imgPtr->numWritten, bytesLeft);
    17561980#if WRITE_DEBUG
    1757             DEBUG("image %d: wrote %d bytes.", imgPtr->id, nWritten);
    1758 #endif
    1759             if (nWritten < 0) {
     1981            DEBUG("image %d: wrote %d bytes.", imgPtr->id, numWritten);
     1982#endif
     1983            if (numWritten < 0) {
    17601984                ERROR("Error writing fd=%d, %s", fd, strerror(errno));
    17611985#if WRITE_DEBUG
     
    17641988                return;
    17651989            }
    1766             bytesLeft -= nWritten;
     1990            bytesLeft -= numWritten;
    17671991            if (bytesLeft > 0) {
    17681992#if WRITE_DEBUG
     
    17711995#endif
    17721996                /* Wrote a short buffer, means we would block. */
    1773                 imgPtr->nWritten += nWritten;
     1997                imgPtr->numWritten += numWritten;
    17741998                imgPtr->bytesLeft = bytesLeft;
    17751999#if WRITE_DEBUG
     
    17782002                return;
    17792003            }
    1780             imgPtr->nWritten += nWritten;
     2004            imgPtr->numWritten += numWritten;
    17812005        }
    17822006        /* Check if image is on the head.  */
     
    17932017}
    17942018
    1795 #if KEEPSTATS
    1796 
    1797 static int
    1798 WriteStats(const char *who, int code)
    1799 {
    1800     double start, finish;
    1801     pid_t pid;
    1802     char buf[BUFSIZ];
    1803     Tcl_DString ds;
    1804 
    1805     {
    1806         struct timeval tv;
    1807 
    1808         /* Get ending time.  */
    1809         gettimeofday(&tv, NULL);
    1810         finish = CVT2SECS(tv);
    1811         tv = stats.start;
    1812         start = CVT2SECS(tv);
    1813     }
    1814     /*
    1815      * Session information:
    1816      *   1. Start date of session in seconds.
    1817      *   2. Process ID
    1818      *   3. Number of frames returned.
    1819      *   4. Number of bytes total returned (in frames).
    1820      *   5. Total elapsed time of all commands.
    1821      *   6. Total elapsed time of session.
    1822      *   7. Exit code of pymol server.
    1823      *   8. User time. 
    1824      *   9. System time.
    1825      *  10. Maximum resident size.
    1826      */
    1827     pid = getpid();
    1828     Tcl_DStringInit(&ds);
    1829    
    1830     sprintf(buf, "<session server=\"%s\" ", who);
    1831     Tcl_DStringAppend(&ds, buf, -1);
    1832 
    1833     strcpy(buf, ctime(&stats.start.tv_sec));
    1834 
    1835     buf[strlen(buf) - 1] = '\0';
    1836     Tcl_DStringAppend(&ds, "date=\"", -1);
    1837     Tcl_DStringAppend(&ds, buf, -1);
    1838     Tcl_DStringAppend(&ds, "\" ", -1);
    1839 
    1840     sprintf(buf, "date_secs=\"%ld\" ", stats.start.tv_sec);
    1841     Tcl_DStringAppend(&ds, buf, -1);
    1842 
    1843     sprintf(buf, "pid=\"%d\" ", pid);
    1844     Tcl_DStringAppend(&ds, buf, -1);
    1845     sprintf(buf, "num_frames=\"%lu\" ", (unsigned long int)stats.nFrames);
    1846     Tcl_DStringAppend(&ds, buf, -1);
    1847     sprintf(buf, "frame_bytes=\"%lu\" ", (unsigned long int)stats.nBytes);
    1848     Tcl_DStringAppend(&ds, buf, -1);
    1849     sprintf(buf, "num_commands=\"%lu\" ", (unsigned long int)stats.nCommands);
    1850     Tcl_DStringAppend(&ds, buf, -1);
    1851     sprintf(buf, "cmd_time=\"%g\" ", stats.cmdTime);
    1852     Tcl_DStringAppend(&ds, buf, -1);
    1853     sprintf(buf, "session_time=\"%g\" ", finish - start);
    1854     Tcl_DStringAppend(&ds, buf, -1);
    1855     sprintf(buf, "status=\"%d\" ", code);
    1856     Tcl_DStringAppend(&ds, buf, -1);
    1857     {
    1858         long clocksPerSec = sysconf(_SC_CLK_TCK);
    1859         double clockRes = 1.0 / clocksPerSec;
    1860         struct tms tms;
    1861 
    1862         memset(&tms, 0, sizeof(tms));
    1863         times(&tms);
    1864         sprintf(buf, "utime=\"%g\" ", tms.tms_utime * clockRes);
    1865         Tcl_DStringAppend(&ds, buf, -1);
    1866         sprintf(buf, "stime=\"%g\" ", tms.tms_stime * clockRes);
    1867         Tcl_DStringAppend(&ds, buf, -1);
    1868         sprintf(buf, "cutime=\"%g\" ", tms.tms_cutime * clockRes);
    1869         Tcl_DStringAppend(&ds, buf, -1);
    1870         sprintf(buf, "cstime=\"%g\" ", tms.tms_cstime * clockRes);
    1871         Tcl_DStringAppend(&ds, buf, -1);
    1872     }
    1873     Tcl_DStringAppend(&ds, "/>\n", -1);
    1874 
    1875     {
    1876         int f;
    1877         ssize_t length;
    1878         int result;
    1879 
    1880 #define STATSDIR        "/var/tmp/visservers"
    1881 #define STATSFILE       STATSDIR "/" "data.xml"
    1882         if (access(STATSDIR, X_OK) != 0) {
    1883             mkdir(STATSDIR, 0770);
    1884         }
    1885         length = Tcl_DStringLength(&ds);
    1886         f = open(STATSFILE, O_APPEND | O_CREAT | O_WRONLY, 0600);
    1887         result = FALSE;
    1888         if (f < 0) {
    1889             goto error;
    1890         }
    1891         if (write(f, Tcl_DStringValue(&ds), length) != length) {
    1892             goto error;
    1893         }
    1894         result = TRUE;
    1895  error:
    1896         if (f >= 0) {
    1897             close(f);
    1898         }
    1899         Tcl_DStringFree(&ds);
    1900         return result;
    1901     }
    1902 }
    1903 #endif
    19042019
    19052020static void
     
    19192034    { "cartoon",        CartoonCmd        },       
    19202035    { "cartoontrace",   CartoonTraceCmd   }, 
     2036    { "clientinfo",     ClientInfoCmd     },
    19212037    { "disable",        DisableCmd        },       
    19222038    { "enable",         EnableCmd         },       
     
    20622178
    20632179    Tcl_DeleteInterp(p->interp);
    2064 #if KEEPSTATS
    2065     WriteStats("pymolproxy", 0);
    2066 #endif
     2180    ServerStats(0);
    20672181
    20682182#if DEBUG
     
    21342248            status = GetLine(&p->client, &numBytes, &line);
    21352249            if (status != BUFFER_OK) {
    2136                 ERROR("can't read client stdout (nBytes=%d): %s\n", numBytes,
     2250                ERROR("can't read client stdout (numBytes=%d): %s\n", numBytes,
    21372251                      strerror(errno));
    21382252                goto done;
     
    22502364                return NULL;
    22512365            }
    2252             stats.nFrames++;
    2253             stats.nBytes += numBytes;
     2366            stats.numFrames++;
     2367            stats.numBytes += numBytes;
    22542368            {
    22552369                struct timeval tv;
Note: See TracChangeset for help on using the changeset viewer.