snprintf(ps_cmd, sizeof(ps_cmd), "ps -o \"%%cpu,cputime\" --no-headers %d", stream->pid);
snprintf(ps_cmd, sizeof(ps_cmd), "ps -o \"%%cpu,bsdtime\" --no-headers %d", stream->pid);
static void compute_stats(HTTPContext *c){    HTTPContext *c1;    FFStream *stream;    char *q, *p;    time_t ti;    int i;    char *new_buffer;    new_buffer = av_malloc(65536);    if (new_buffer) {        av_free(c->buffer);        c->buffer_size = 65536;        c->buffer = new_buffer;        c->buffer_ptr = c->buffer;        c->buffer_end = c->buffer + c->buffer_size;    }    q = c->buffer;    q += sprintf(q, "HTTP/1.0 200 OK\r\n");    q += sprintf(q, "Content-type: %s\r\n", "text/html");    q += sprintf(q, "Pragma: no-cache\r\n");    q += sprintf(q, "\r\n");        q += sprintf(q, "<HEAD><TITLE>FFServer Status</TITLE>\n");    if (c->stream->feed_filename) {        q += sprintf(q, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);    }    q += sprintf(q, "</HEAD>\n<BODY>");    q += sprintf(q, "<H1>FFServer Status</H1>\n");    /* format status */    q += sprintf(q, "<H2>Available Streams</H2>\n");    q += sprintf(q, "<TABLE cellspacing=0 cellpadding=4>\n");    q += sprintf(q, "<TR><Th valign=top>Path<th align=left>Served<br>Conns<Th><br>bytes<Th valign=top>Format<Th>Bit rate<br>kbits/s<Th align=left>Video<br>kbits/s<th><br>Codec<Th align=left>Audio<br>kbits/s<th><br>Codec<Th align=left valign=top>Feed\n");    stream = first_stream;    while (stream != NULL) {        char sfilename[1024];        char *eosf;        if (stream->feed != stream) {            pstrcpy(sfilename, sizeof(sfilename) - 1, stream->filename);            eosf = sfilename + strlen(sfilename);            if (eosf - sfilename >= 4) {                if (strcmp(eosf - 4, ".asf") == 0) {                    strcpy(eosf - 4, ".asx");                } else if (strcmp(eosf - 3, ".rm") == 0) {                    strcpy(eosf - 3, ".ram");                }            }                        q += sprintf(q, "<TR><TD><A HREF=\"/%s\">%s</A> ",                          sfilename, stream->filename);            q += sprintf(q, "<td align=right> %d <td align=right> ",                        stream->conns_served);            q += fmt_bytecount(q, stream->bytes_served);            switch(stream->stream_type) {            case STREAM_TYPE_LIVE:                {                    int audio_bit_rate = 0;                    int video_bit_rate = 0;                    char *audio_codec_name = "";                    char *video_codec_name = "";                    char *audio_codec_name_extra = "";                    char *video_codec_name_extra = "";                    for(i=0;i<stream->nb_streams;i++) {                        AVStream *st = stream->streams[i];                        AVCodec *codec = avcodec_find_encoder(st->codec.codec_id);                        switch(st->codec.codec_type) {                        case CODEC_TYPE_AUDIO:                            audio_bit_rate += st->codec.bit_rate;                            if (codec) {                                if (*audio_codec_name)                                    audio_codec_name_extra = "...";                                audio_codec_name = codec->name;                            }                            break;                        case CODEC_TYPE_VIDEO:                            video_bit_rate += st->codec.bit_rate;                            if (codec) {                                if (*video_codec_name)                                    video_codec_name_extra = "...";                                video_codec_name = codec->name;                            }                            break;                        default:                            av_abort();                        }                    }                    q += sprintf(q, "<TD align=center> %s <TD align=right> %d <TD align=right> %d <TD> %s %s <TD align=right> %d <TD> %s %s",                                  stream->fmt->name,                                 (audio_bit_rate + video_bit_rate) / 1000,                                 video_bit_rate / 1000, video_codec_name, video_codec_name_extra,                                 audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);                    if (stream->feed) {                        q += sprintf(q, "<TD>%s", stream->feed->filename);                    } else {                        q += sprintf(q, "<TD>%s", stream->feed_filename);                    }                    q += sprintf(q, "\n");                }                break;            default:                q += sprintf(q, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");                break;            }        }        stream = stream->next;    }    q += sprintf(q, "</TABLE>\n");    stream = first_stream;    while (stream != NULL) {        if (stream->feed == stream) {            q += sprintf(q, "<h2>Feed %s</h2>", stream->filename);            if (stream->pid) {                FILE *pid_stat;                char ps_cmd[64];                q += sprintf(q, "Running as pid %d.\n", stream->pid);#ifdef linux                /* This is somewhat linux specific I guess */                snprintf(ps_cmd, sizeof(ps_cmd), "ps -o \"%%cpu,cputime\" --no-headers %d", stream->pid);                pid_stat = popen(ps_cmd, "r");                if (pid_stat) {                    char cpuperc[10];                    char cpuused[64];                    if (fscanf(pid_stat, "%10s %64s", cpuperc, cpuused) == 2) {                        q += sprintf(q, "Currently using %s%% of the cpu. Total time used %s.\n",                            cpuperc, cpuused);                    }                    fclose(pid_stat);                }#endif                q += sprintf(q, "<p>");            }            q += sprintf(q, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>type<th>kbits/s<th align=left>codec<th align=left>Parameters\n");            for (i = 0; i < stream->nb_streams; i++) {                AVStream *st = stream->streams[i];                AVCodec *codec = avcodec_find_encoder(st->codec.codec_id);                char *type = "unknown";                char parameters[64];                parameters[0] = 0;                switch(st->codec.codec_type) {                case CODEC_TYPE_AUDIO:                    type = "audio";                    break;                case CODEC_TYPE_VIDEO:                    type = "video";                    sprintf(parameters, "%dx%d, q=%d-%d, fps=%d", st->codec.width, st->codec.height,                                st->codec.qmin, st->codec.qmax, st->codec.frame_rate / FRAME_RATE_BASE);                    break;                default:                    av_abort();                }                q += sprintf(q, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",                        i, type, st->codec.bit_rate/1000, codec ? codec->name : "", parameters);            }            q += sprintf(q, "</table>\n");        }               stream = stream->next;    }    #if 0    {        float avg;        AVCodecContext *enc;        char buf[1024];                /* feed status */        stream = first_feed;        while (stream != NULL) {            q += sprintf(q, "<H1>Feed '%s'</H1>\n", stream->filename);            q += sprintf(q, "<TABLE>\n");            q += sprintf(q, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");            for(i=0;i<stream->nb_streams;i++) {                AVStream *st = stream->streams[i];                FeedData *fdata = st->priv_data;                enc = &st->codec;                            avcodec_string(buf, sizeof(buf), enc);                avg = fdata->avg_frame_size * (float)enc->rate * 8.0;                if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)                    avg /= enc->frame_size;                q += sprintf(q, "<TR><TD>%s <TD> %d <TD> %Ld <TD> %0.1f\n",                              buf, enc->frame_number, fdata->data_count, avg / 1000.0);            }            q += sprintf(q, "</TABLE>\n");            stream = stream->next_feed;        }    }#endif    /* connection status */    q += sprintf(q, "<H2>Connection Status</H2>\n");    q += sprintf(q, "Number of connections: %d / %d<BR>\n",                 nb_connections, nb_max_connections);    q += sprintf(q, "Bandwidth in use: %dk / %dk<BR>\n",                 nb_bandwidth, nb_max_bandwidth);    q += sprintf(q, "<TABLE>\n");    q += sprintf(q, "<TR><Th>#<Th>File<Th>IP<Th>State<Th>kbits/sec<Th>Size\n");    c1 = first_http_ctx;    i = 0;    while (c1 != NULL && q < (char *) c->buffer + c->buffer_size - 2048) {        int bitrate;        int j;        bitrate = 0;        for (j = 0; j < c1->stream->nb_streams; j++) {            if (c1->feed_streams[j] >= 0) {                bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec.bit_rate;            }        }        i++;        p = inet_ntoa(c1->from_addr.sin_addr);        q += sprintf(q, "<TR><TD><B>%d</B><TD>%s%s <TD> %s <TD> %s <td align=right> %d <TD align=right> ",                      i, c1->stream->filename,                      c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",                     p,                      http_state[c1->state],                     bitrate / 1000);        q += fmt_bytecount(q, c1->data_count);        *q++ = '\n';        c1 = c1->next;    }    q += sprintf(q, "</TABLE>\n");        /* date */    ti = time(NULL);    p = ctime(&ti);    q += sprintf(q, "<HR size=1 noshade>Generated at %s", p);    q += sprintf(q, "</BODY>\n</HTML>\n");    c->buffer_ptr = c->buffer;    c->buffer_end = q;}