sssimulator / td.c @ master
History | View | Annotate | Download (4.29 KB)
1 | 86681e55 | luca | #include <stdlib.h> |
---|---|---|---|
2 | #include <stdio.h> |
||
3 | #include <string.h> |
||
4 | f850347b | Luca Baldesi | #include <stdint.h> |
5 | 86681e55 | luca | |
6 | #include "core.h" |
||
7 | #include "sched.h" |
||
8 | #include "stats.h" |
||
9 | #include "td.h" |
||
10 | |||
11 | FILE *resfile; |
||
12 | 80f4c362 | Luca Baldesi | FILE *delayfile; |
13 | 86681e55 | luca | FILE *statusfile; |
14 | static int ts; |
||
15 | static struct peer *peers; |
||
16 | static struct chunk *chunks; |
||
17 | static int num_peers; |
||
18 | 32c9a7bc | Luca Baldesi | extern float convergence_precision; |
19 | f850347b | Luca Baldesi | extern uint32_t analysis_window;
|
20 | 78d8960f | Luca Baldesi | extern float netload; |
21 | 86681e55 | luca | |
22 | int comp(int chunk) |
||
23 | { |
||
24 | return (chunks[chunk].received == num_peers);
|
||
25 | } |
||
26 | |||
27 | |||
28 | ///==========================================================
|
||
29 | /// the network model :)
|
||
30 | ///==========================================================
|
||
31 | //chunks propagate through receiver's recv_chunks array
|
||
32 | static void chunk_send(int k, struct peer *p, int dl) |
||
33 | { |
||
34 | #if 0
|
||
35 | if (p->chunks[k]) {
|
||
36 | fprintf(stderr, "Error: sending chunk %d to %d which already has it!\n", k, p->id);
|
||
37 | exit(-1);
|
||
38 | }
|
||
39 | #endif
|
||
40 | if (p->recv_chunks[win_idx(p, k)]) {
|
||
41 | fprintf(stderr, "Error: sending chunk %d (idx: %d) to %d which already receives it (%d)!\n", k, win_idx(p, k), p->id, p->recv_chunks[k]->chunk);
|
||
42 | exit(-1);
|
||
43 | } |
||
44 | if (p->chunks[win_idx(p, k)]) {
|
||
45 | p->recv_chunks[win_idx(p, k)] = p->chunks[win_idx(p, k)]; |
||
46 | p->chunks[win_idx(p, k)] = NULL;
|
||
47 | } else {
|
||
48 | p->recv_chunks[win_idx(p, k)] = malloc(sizeof(struct chunk_instance)); |
||
49 | } |
||
50 | p->recv_chunks[win_idx(p, k)]->dl = dl; |
||
51 | p->recv_chunks[win_idx(p, k)]->chunk = k; |
||
52 | |||
53 | //CACHE latest_not_needed
|
||
54 | p->latest_not_needed = max(k,p->latest_not_needed); |
||
55 | chunks[k].received += 1;
|
||
56 | } |
||
57 | |||
58 | static int receive(int t) |
||
59 | { |
||
60 | int p, c;
|
||
61 | int cnt;
|
||
62 | |||
63 | cnt = 0;
|
||
64 | for (p = 0; p < num_peers; p++) { |
||
65 | for (c = 0; c < peers[p].buf_size; c++) { |
||
66 | int idx = win_idx(&peers[p], c /* + peers[p].buf_size*/); |
||
67 | |||
68 | if (peers[p].recv_chunks[idx]) {
|
||
69 | cnt++; |
||
70 | peers[p].chunks[idx] = peers[p].recv_chunks[idx]; |
||
71 | peers[p].chunks[idx]->completed = t; |
||
72 | peers[p].recv_chunks[idx] = NULL;
|
||
73 | chunks[peers[p].chunks[idx]->chunk].avg_delay += t - peers[p].chunks[idx]->chunk; |
||
74 | chunks[peers[p].chunks[idx]->chunk].max_delay = max(chunks[peers[p].chunks[idx]->chunk].max_delay, t - peers[p].chunks[idx]->chunk); |
||
75 | 80f4c362 | Luca Baldesi | if (delayfile) {
|
76 | fprintf(delayfile, "%d,%d,%d,%d\n", t, p, peers[p].chunks[idx]->chunk, t - peers[p].chunks[idx]->chunk);
|
||
77 | fflush(delayfile); |
||
78 | } |
||
79 | 86681e55 | luca | if (chunks[peers[p].chunks[idx]->chunk].received == num_peers) {
|
80 | if (resfile) {
|
||
81 | fprintf(resfile, "Chunk %d received at %d in %d\n", peers[p].chunks[idx]->chunk, t, t - peers[p].chunks[idx]->chunk);
|
||
82 | fflush(resfile); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | #if 0
|
||
89 | for (c = 0; c < num_chunks; j++) {
|
||
90 | if (completed[c] == 0) {
|
||
91 | completed[c] = t;
|
||
92 | for (p = 0; p < num_peers; p++) {
|
||
93 | if (peers[p].chunks[c] == NULL) {
|
||
94 | completed[c] = 0;
|
||
95 | }
|
||
96 | }
|
||
97 | }
|
||
98 | }
|
||
99 | #endif
|
||
100 | |||
101 | return cnt;
|
||
102 | } |
||
103 | |||
104 | struct chunk *td_loop(struct peer *p, int np, int num_chunks, int server_period) |
||
105 | { |
||
106 | 78d8960f | Luca Baldesi | int done, t, offers;
|
107 | 32c9a7bc | Luca Baldesi | struct stats * stat = NULL; |
108 | 78d8960f | Luca Baldesi | |
109 | offers = ((int)netload) > 0 ? ((int)netload) : 1; |
||
110 | 86681e55 | luca | |
111 | chunks = malloc(num_chunks * sizeof(struct chunk)); |
||
112 | if (chunks == NULL) { |
||
113 | perror("MAlloc");
|
||
114 | |||
115 | return NULL; |
||
116 | } |
||
117 | 52770a8f | Luca Baldesi | memset(chunks, 0, num_chunks * sizeof(struct chunk)); |
118 | 86681e55 | luca | num_peers = np; |
119 | peers = p; |
||
120 | ts = server_period; |
||
121 | |||
122 | 52770a8f | Luca Baldesi | done = 0; t = 0; // t is the time in chunk time (discrete) |
123 | 86681e55 | luca | while (!done) {
|
124 | 78d8960f | Luca Baldesi | int i, j;
|
125 | 86681e55 | luca | struct peer *target;
|
126 | |||
127 | 52770a8f | Luca Baldesi | // loop stuff
|
128 | 86681e55 | luca | done = ((t != 0) && (receive(t) == 0)); |
129 | 52770a8f | Luca Baldesi | status_print(statusfile, peers, num_peers, chunks, num_chunks, t); |
130 | 86681e55 | luca | if (trace) fprintf(trace, "Time %d:\n", t); |
131 | 52770a8f | Luca Baldesi | |
132 | // source seeding
|
||
133 | 86681e55 | luca | source_send(t, &target); |
134 | if (target != NULL) { |
||
135 | chunk_send(t, target, t + ts); |
||
136 | } |
||
137 | 52770a8f | Luca Baldesi | |
138 | // peer offering
|
||
139 | 86681e55 | luca | for (i = 0; i < num_peers; i++) { |
140 | int chunk;
|
||
141 | |||
142 | 78d8960f | Luca Baldesi | for (j = 0; j < offers; j++) |
143 | { |
||
144 | peer_send(&peers[i], t, &chunk, &target); |
||
145 | if (target != NULL) { |
||
146 | peers[i].chunks[win_idx(&peers[i], chunk)]->dl += ts; |
||
147 | chunk_send(chunk, target, peers[i].chunks[win_idx(&peers[i], chunk)]->dl); |
||
148 | } |
||
149 | 86681e55 | luca | } |
150 | } |
||
151 | t++; |
||
152 | f850347b | Luca Baldesi | if (convergence_precision && chunk_stats_converge(&stat, chunks, num_chunks, peers, num_peers, t - 1, analysis_window)) |
153 | 32c9a7bc | Luca Baldesi | done = 1;
|
154 | 86681e55 | luca | } |
155 | f850347b | Luca Baldesi | chunk_stats_converge(&stat, chunks, num_chunks, peers, num_peers, t - 1, analysis_window);
|
156 | 32c9a7bc | Luca Baldesi | chunk_stats_print(stat, CSV_STATS); |
157 | if (stat) free(stat);
|
||
158 | 86681e55 | luca | |
159 | return chunks;
|
||
160 | } |