Klaus Demo nginx / 0d59500
Event pipe: reduced number of file buffers used. If possible we now just extend already present file buffer in p->out chain instead of keeping ngx_buf_t for each buffer we've flushed to disk. This saves about 120 bytes of memory per buffer flushed to disk, and resolves high CPU usage observed in edge cases (due to coalescing these buffers on send). Maxim Dounin 10 years ago
2 changed file(s) with 59 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
679679 static ngx_int_t
680680 ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p)
681681 {
682 ssize_t size, bsize;
682 ssize_t size, bsize, n;
683683 ngx_buf_t *b;
684684 ngx_uint_t prev_last_shadow;
685 ngx_chain_t *cl, *tl, *next, *out, **ll, **last_free, fl;
685 ngx_chain_t *cl, *tl, *next, *out, **ll, **last_out, **last_free, fl;
686686
687687 if (p->buf_to_file) {
688688 fl.buf = p->buf_to_file;
747747 p->last_in = &p->in;
748748 }
749749
750 if (ngx_write_chain_to_temp_file(p->temp_file, out) == NGX_ERROR) {
750 n = ngx_write_chain_to_temp_file(p->temp_file, out);
751
752 if (n == NGX_ERROR) {
751753 return NGX_ABORT;
752754 }
755
756 if (p->buf_to_file) {
757 p->temp_file->offset = p->buf_to_file->last - p->buf_to_file->pos;
758 n -= p->buf_to_file->last - p->buf_to_file->pos;
759 p->buf_to_file = NULL;
760 out = out->next;
761 }
762
763 if (n > 0) {
764 /* update previous buffer or add new buffer */
765
766 if (p->out) {
767 for (cl = p->out; cl->next; cl = cl->next) { /* void */ }
768
769 b = cl->buf;
770
771 if (b->file_last == p->temp_file->offset) {
772 p->temp_file->offset += n;
773 b->file_last = p->temp_file->offset;
774 goto free;
775 }
776
777 last_out = &cl->next;
778
779 } else {
780 last_out = &p->out;
781 }
782
783 cl = ngx_chain_get_free_buf(p->pool, &p->free);
784 if (cl == NULL) {
785 return NGX_ABORT;
786 }
787
788 b = cl->buf;
789
790 ngx_memzero(b, sizeof(ngx_buf_t));
791
792 b->tag = p->tag;
793
794 b->file = &p->temp_file->file;
795 b->file_pos = p->temp_file->offset;
796 p->temp_file->offset += n;
797 b->file_last = p->temp_file->offset;
798
799 b->in_file = 1;
800 b->temp_file = 1;
801
802 *last_out = cl;
803 }
804
805 free:
753806
754807 for (last_free = &p->free_raw_bufs;
755808 *last_free != NULL;
758811 /* void */
759812 }
760813
761 if (p->buf_to_file) {
762 p->temp_file->offset = p->buf_to_file->last - p->buf_to_file->pos;
763 p->buf_to_file = NULL;
764 out = out->next;
765 }
766
767814 for (cl = out; cl; cl = next) {
768815 next = cl->next;
769 cl->next = NULL;
816
817 cl->next = p->free;
818 p->free = cl;
770819
771820 b = cl->buf;
772 b->file = &p->temp_file->file;
773 b->file_pos = p->temp_file->offset;
774 p->temp_file->offset += b->last - b->pos;
775 b->file_last = p->temp_file->offset;
776
777 b->in_file = 1;
778 b->temp_file = 1;
779
780 if (p->out) {
781 *p->last_out = cl;
782 } else {
783 p->out = cl;
784 }
785 p->last_out = &cl->next;
786821
787822 if (b->last_shadow) {
788823
2929 ngx_chain_t **last_in;
3030
3131 ngx_chain_t *out;
32 ngx_chain_t **last_out;
33
3432 ngx_chain_t *free;
3533 ngx_chain_t *busy;
3634