nxt_port_memory.c (197:ae8f843e1fd4) nxt_port_memory.c (206:86a529b2ea9b)
1
2/*
3 * Copyright (C) Max Romanov
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <nxt_main.h>
8

--- 81 unchanged lines hidden (view full) ---

90 nxt_port_mmap_header_t *hdr;
91
92 if (nxt_buf_ts_handle(task, obj, data)) {
93 return;
94 }
95
96 b = obj;
97
1
2/*
3 * Copyright (C) Max Romanov
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <nxt_main.h>
8

--- 81 unchanged lines hidden (view full) ---

90 nxt_port_mmap_header_t *hdr;
91
92 if (nxt_buf_ts_handle(task, obj, data)) {
93 return;
94 }
95
96 b = obj;
97
98 nxt_debug(task, "mmap buf completion: %p %p", b, b->mem.start);
99
100 mp = b->data;
101
102#if (NXT_DEBUG)
103 if (nxt_slow_path(data != b->parent)) {
104 nxt_log_alert(task->log, "completion data (%p) != b->parent (%p)",
105 data, b->parent);
106 nxt_abort();
107 }

--- 12 unchanged lines hidden (view full) ---

120
121 } else {
122 p = b->mem.start;
123 c = nxt_port_mmap_chunk_id(hdr, p);
124 }
125
126 nxt_port_mmap_free_junk(p, b->mem.end - p);
127
98 mp = b->data;
99
100#if (NXT_DEBUG)
101 if (nxt_slow_path(data != b->parent)) {
102 nxt_log_alert(task->log, "completion data (%p) != b->parent (%p)",
103 data, b->parent);
104 nxt_abort();
105 }

--- 12 unchanged lines hidden (view full) ---

118
119 } else {
120 p = b->mem.start;
121 c = nxt_port_mmap_chunk_id(hdr, p);
122 }
123
124 nxt_port_mmap_free_junk(p, b->mem.end - p);
125
126 nxt_debug(task, "mmap buf completion: %p [%p,%d] (sent=%d), %PI,%d,%d", b,
127 b->mem.start, b->mem.end - b->mem.start, b->is_port_mmap_sent,
128 hdr->pid, hdr->id, c);
129
128 while (p < b->mem.end) {
129 nxt_port_mmap_set_chunk_free(hdr, c);
130
131 p += PORT_MMAP_CHUNK_SIZE;
132 c++;
133 }
134
135 nxt_mp_release(mp, b);

--- 273 unchanged lines hidden (view full) ---

409{
410 size_t nchunks;
411 nxt_buf_t *b;
412 nxt_chunk_id_t c;
413 nxt_port_mmap_header_t *hdr;
414
415 nxt_debug(task, "request %z bytes shm buffer", size);
416
130 while (p < b->mem.end) {
131 nxt_port_mmap_set_chunk_free(hdr, c);
132
133 p += PORT_MMAP_CHUNK_SIZE;
134 c++;
135 }
136
137 nxt_mp_release(mp, b);

--- 273 unchanged lines hidden (view full) ---

411{
412 size_t nchunks;
413 nxt_buf_t *b;
414 nxt_chunk_id_t c;
415 nxt_port_mmap_header_t *hdr;
416
417 nxt_debug(task, "request %z bytes shm buffer", size);
418
417 if (nxt_slow_path(size > PORT_MMAP_DATA_SIZE)) {
418 nxt_debug(task, "requested size (%z bytes) too big", size);
419 return NULL;
420 }
421
422 b = nxt_buf_mem_ts_alloc(task, port->mem_pool, 0);
423 if (nxt_slow_path(b == NULL)) {
424 return NULL;
425 }
426
427 b->completion_handler = nxt_port_mmap_buf_completion;
428 nxt_buf_set_port_mmap(b);
429

--- 10 unchanged lines hidden (view full) ---

440 b->mem.free = b->mem.start;
441 b->mem.end = b->mem.start + PORT_MMAP_CHUNK_SIZE;
442
443 nchunks = size / PORT_MMAP_CHUNK_SIZE;
444 if ((size % PORT_MMAP_CHUNK_SIZE) != 0 || nchunks == 0) {
445 nchunks++;
446 }
447
419 b = nxt_buf_mem_ts_alloc(task, port->mem_pool, 0);
420 if (nxt_slow_path(b == NULL)) {
421 return NULL;
422 }
423
424 b->completion_handler = nxt_port_mmap_buf_completion;
425 nxt_buf_set_port_mmap(b);
426

--- 10 unchanged lines hidden (view full) ---

437 b->mem.free = b->mem.start;
438 b->mem.end = b->mem.start + PORT_MMAP_CHUNK_SIZE;
439
440 nchunks = size / PORT_MMAP_CHUNK_SIZE;
441 if ((size % PORT_MMAP_CHUNK_SIZE) != 0 || nchunks == 0) {
442 nchunks++;
443 }
444
445 nxt_debug(task, "outgoing mmap buf allocation: %p [%p,%d] %PI,%d,%d", b,
446 b->mem.start, b->mem.end - b->mem.start,
447 hdr->pid, hdr->id, c);
448
448 c++;
449 nchunks--;
450
451 /* Try to acquire as much chunks as required. */
452 while (nchunks > 0) {
453
454 if (nxt_port_mmap_chk_set_chunk_busy(hdr, c) == 0) {
455 break;

--- 4 unchanged lines hidden (view full) ---

460 nchunks--;
461 }
462
463 return b;
464}
465
466
467nxt_int_t
449 c++;
450 nchunks--;
451
452 /* Try to acquire as much chunks as required. */
453 while (nchunks > 0) {
454
455 if (nxt_port_mmap_chk_set_chunk_busy(hdr, c) == 0) {
456 break;

--- 4 unchanged lines hidden (view full) ---

461 nchunks--;
462 }
463
464 return b;
465}
466
467
468nxt_int_t
468nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size)
469nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size,
470 size_t min_size)
469{
471{
470 size_t nchunks;
472 size_t nchunks, free_size;
471 nxt_chunk_id_t c, start;
472 nxt_port_mmap_header_t *hdr;
473
474 nxt_debug(task, "request increase %z bytes shm buffer", size);
475
476 if (nxt_slow_path(nxt_buf_is_port_mmap(b) == 0)) {
477 nxt_log(task, NXT_LOG_WARN,
478 "failed to increase, not a mmap buffer");
479 return NXT_ERROR;
480 }
481
473 nxt_chunk_id_t c, start;
474 nxt_port_mmap_header_t *hdr;
475
476 nxt_debug(task, "request increase %z bytes shm buffer", size);
477
478 if (nxt_slow_path(nxt_buf_is_port_mmap(b) == 0)) {
479 nxt_log(task, NXT_LOG_WARN,
480 "failed to increase, not a mmap buffer");
481 return NXT_ERROR;
482 }
483
482 if (nxt_slow_path(size <= (size_t) nxt_buf_mem_free_size(&b->mem))) {
484 free_size = nxt_buf_mem_free_size(&b->mem);
485
486 if (nxt_slow_path(size <= free_size)) {
483 return NXT_OK;
484 }
485
486 hdr = b->parent;
487
488 start = nxt_port_mmap_chunk_id(hdr, b->mem.end);
489
487 return NXT_OK;
488 }
489
490 hdr = b->parent;
491
492 start = nxt_port_mmap_chunk_id(hdr, b->mem.end);
493
490 size -= nxt_buf_mem_free_size(&b->mem);
494 size -= free_size;
491
492 nchunks = size / PORT_MMAP_CHUNK_SIZE;
493 if ((size % PORT_MMAP_CHUNK_SIZE) != 0 || nchunks == 0) {
494 nchunks++;
495 }
496
497 c = start;
498
499 /* Try to acquire as much chunks as required. */
500 while (nchunks > 0) {
501
502 if (nxt_port_mmap_chk_set_chunk_busy(hdr, c) == 0) {
503 break;
504 }
505
506 c++;
507 nchunks--;
508 }
509
495
496 nchunks = size / PORT_MMAP_CHUNK_SIZE;
497 if ((size % PORT_MMAP_CHUNK_SIZE) != 0 || nchunks == 0) {
498 nchunks++;
499 }
500
501 c = start;
502
503 /* Try to acquire as much chunks as required. */
504 while (nchunks > 0) {
505
506 if (nxt_port_mmap_chk_set_chunk_busy(hdr, c) == 0) {
507 break;
508 }
509
510 c++;
511 nchunks--;
512 }
513
510 if (nchunks != 0) {
514 if (nchunks != 0 &&
515 min_size > free_size + PORT_MMAP_CHUNK_SIZE * (c - start)) {
516
511 c--;
512 while (c >= start) {
513 nxt_port_mmap_set_chunk_free(hdr, c);
514 c--;
515 }
516
517 nxt_debug(task, "failed to increase, %d chunks busy", nchunks);
518

--- 35 unchanged lines hidden (view full) ---

554
555 b->mem.start = nxt_port_mmap_chunk_start(hdr, mmap_msg->chunk_id);
556 b->mem.pos = b->mem.start;
557 b->mem.free = b->mem.start + mmap_msg->size;
558 b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE;
559
560 b->parent = hdr;
561
517 c--;
518 while (c >= start) {
519 nxt_port_mmap_set_chunk_free(hdr, c);
520 c--;
521 }
522
523 nxt_debug(task, "failed to increase, %d chunks busy", nchunks);
524

--- 35 unchanged lines hidden (view full) ---

560
561 b->mem.start = nxt_port_mmap_chunk_start(hdr, mmap_msg->chunk_id);
562 b->mem.pos = b->mem.start;
563 b->mem.free = b->mem.start + mmap_msg->size;
564 b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE;
565
566 b->parent = hdr;
567
568 nxt_debug(task, "incoming mmap buf allocation: %p [%p,%d] %PI,%d,%d", b,
569 b->mem.start, b->mem.end - b->mem.start,
570 hdr->pid, hdr->id, mmap_msg->chunk_id);
571
562 return b;
563}
564
565
566void
567nxt_port_mmap_write(nxt_task_t *task, nxt_port_t *port,
568 nxt_port_send_msg_t *msg, nxt_sendbuf_coalesce_t *sb)
569{

--- 147 unchanged lines hidden ---
572 return b;
573}
574
575
576void
577nxt_port_mmap_write(nxt_task_t *task, nxt_port_t *port,
578 nxt_port_send_msg_t *msg, nxt_sendbuf_coalesce_t *sb)
579{

--- 147 unchanged lines hidden ---