summaryrefslogtreecommitdiffstats
path: root/src/stateless/cp/trex_vm_splitter.cpp
blob: 489e3b757fc82326e04496143b7da79b65abc7d5 (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
/*
 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>

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;

    /* 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() {

    const StreamVmInstruction *split_instr = m_stream->m_vm.get_split_instruction();
    if (split_instr == NULL) {
        return false;
    }

    if (split_instr->get_instruction_type() == StreamVmInstruction::itFLOW_MAN) {
        return split_by_flow_var( (const StreamVmInstructionFlowMan *)split_instr );

    } else if (split_instr->get_instruction_type() == StreamVmInstruction::itFLOW_CLIENT) {
        return split_by_flow_client_var( (const StreamVmInstructionFlowClient *)split_instr );

    } else {
        throw TrexException("VM splitter : cannot split by instruction which is not flow var or flow client var");
    }

}

/**
 * split VM by flow var 
 * 
 * @author imarom (20-Dec-15)
 * 
 * @param instr 
 * 
 * @return bool 
 */
bool
TrexVmSplitter::split_by_flow_var(const StreamVmInstructionFlowMan *instr) {
    /* no point in splitting random */
    if (instr->m_op == StreamVmInstructionFlowMan::FLOW_VAR_OP_RANDOM) {
        return false;
    }

    /* if the range is too small - multiply  */
    if (instr->get_range() < m_dp_core_count) {
        // FIXME
        return false;
    }

    /* we need to split - duplicate VM now */
    duplicate_vm();

    /* calculate range splitting */
    uint64_t range = instr->get_range();

    uint64_t range_part = range / m_dp_core_count;
    uint64_t leftover   = range % m_dp_core_count;

    /* first core handles a bit more */
    uint64_t start   = instr->m_min_value;
    uint64_t end     = start + range_part + leftover - 1;


    /* do work */
    for (TrexStream *core_stream : *m_core_streams) {

        /* get the per-core instruction to split */
        StreamVmInstructionFlowMan *per_core_instr = (StreamVmInstructionFlowMan *)core_stream->m_vm.get_split_instruction();

        per_core_instr->m_min_value  = start;
        per_core_instr->m_max_value  = end; 

        /* init value is the max value because the VM program's first iteration */
        per_core_instr->m_init_value = end;

        core_stream->vm_compile();

        start = end + 1;
        end   = start + range_part - 1;
    }

    return true;
}


bool
TrexVmSplitter::split_by_flow_client_var(const StreamVmInstructionFlowClient *instr) {
    return false;
}


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