summaryrefslogtreecommitdiffstats
path: root/src/stateless/cp/trex_vm_splitter.cpp
blob: 0c2edd88247f32837e4daeeadaa1e5f24fe04f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 Itay Marom
 Cisco Systems, Inc.
*/

/*
Copyright (c) 2015-2015 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <trex_vm_splitter.h>
#include <trex_stateless.h>

/**
 * split a specific stream's VM to multiple cores
 * number of cores is implied by the size of the vector
 * 
 */
void
TrexVmSplitter::split(TrexStream *stream, std::vector<TrexStream *> core_streams) {

    /* nothing to do if no VM */
    if (stream->m_vm.is_vm_empty()) {
        return;
    }

    /* prepare some vars */
    m_dp_core_count = core_streams.size();
    m_core_streams  = &core_streams;
    m_stream        = stream;

    uint16_t cache_size=m_stream->m_cache_size;
    /* split the cache_size  */
    if (cache_size>0) {

        /* TBD need to check if we need to it is not too big from pool */
        if (cache_size > 10000) {
            throw TrexException("Cache is too big try to reduce it ");
        }

        /* split like variable splitters with leftovers */
        uint16_t cache_per_core = cache_size/m_dp_core_count;
        uint16_t leftover   = cache_size % m_dp_core_count;

        if (cache_per_core<1) {
            cache_per_core=1;
            leftover=0;
        }

        for (TrexStream *core_stream : *m_core_streams) {
            core_stream->m_cache_size = cache_per_core;
            if (leftover) {
                core_stream->m_cache_size+=1; 
                leftover-=1;
            }
        }
    }


    /* if we cannot split - compile the main and duplicate */
    bool rc = split_internal();
    if (!rc) {

        /* compile the stream and simply clone it to all streams */
        m_stream->vm_compile();

        /* for every core - simply clone the DP object */
        for (TrexStream *core_stream : *m_core_streams) {
            core_stream->m_vm_dp = m_stream->m_vm_dp->clone();
        }

        /* no need for the reference stream DP object */
        delete m_stream->m_vm_dp;
        m_stream->m_vm_dp = NULL;
    }
}

bool
TrexVmSplitter::split_internal() {

    /* no split needed ? fall back */
    if (!m_stream->m_vm.need_split()) {
        return false;
    }

    duplicate_vm();

    /* search for splitable instructions */
    for (StreamVmInstruction *instr : m_stream->m_vm.get_instruction_list()) {
        if (!instr->is_var_instruction()) {
            continue;
        }

        split_flow_var( (const StreamVmInstructionVar *)instr );

    }


    /* done - now compile for all cores */
    compile_vm();

    return true;

}

/**
 * split a flow var instruction
 * 
 * @author imarom (20-Dec-15)
 * 
 * @param instr 
 * 
 * @return bool 
 */
void
TrexVmSplitter::split_flow_var(const StreamVmInstructionVar *src) {
    /* a var might not need split (random) */
    if (!src->need_split()) {
        return;
    }

    /* do work */
    int core_id = 0;
    for (TrexStream *core_stream : *m_core_streams) {

        StreamVmInstructionVar *dst = core_stream->m_vm.lookup_var_by_name(src->get_var_name());
        assert(dst);

        /* for each core we need to give a phase and multiply the step frequency */
        dst->update(core_id, m_dp_core_count);

        core_id++;
    }

}



/**
 * duplicate the VM instructions
 * to all the cores
 */
void
TrexVmSplitter::duplicate_vm() {
    /* for each core - duplicate the instructions */
    for (TrexStream *core_stream : *m_core_streams) {
        m_stream->m_vm.clone(core_stream->m_vm);
    }
}

/**
 * now compile the updated VM
 */
void
TrexVmSplitter::compile_vm() {
    /* for each core - duplicate the instructions */
    for (TrexStream *core_stream : *m_core_streams) {
        core_stream->vm_compile();
    }
}