# Secure Partitioning of Cloud Applications, with Cost Look-Ahead
## 6 Partitioning with a Look Ahead on Migration Costs
### Probabilistic SKnife  
ProbSKnife is a declarative prototype that builds on SKnife to determine safe partitionings and contains new Prolog predicates to determine the migration costs and new labellings. Moreover, it includes a Python script to execute multiple Prolog queries, parse the outputs and aggregate the results. The Python code allows us to simplify the execution flow of ProbSKnife and it is more suited than Prolog to calculate the probabilistic model and the future cost. The exponential dimension of the calculation makes it infeasible to use a pure Prolog prototype, so our choice is to mix our declarative modelling and the determination of partitionings, costs and evolving labelling written in Prolog and the execution flow and expected cost processing written in Python. To find all the partitionings that satisfy a labelling with a limit on the number of domains, we use the refined predicate sKnife/4, listed in Listing 7. ```
43 knife(ApId,Labelling,DLimit,Partitioning):-
44 application(ApId,Hardware,Software),
45 hardwareOK(Labelling,Hardware),
46 softwareLabel(Labelling,Software,LabelledSoftware),
47 softwareOK(Labelling,LabelledSoftware),
48 partitioning(LabelledSoftware,0,DLimit,[],Partitioning). ```  
Listing 7: The sKnife/4 predicate. The new variable DLimit (\(d\) in the above formalisation) represents the maximum number of domains admitted for a partitioning. This variable is used to find a safe partitioning (line 48), together with the initial number of domains (0). The partitioning/5 predicate is listed in Listing 8, with the main differences between the previous version of Listing 5 coloured in cyan. As before, the predicate has two main clauses that scan the list of labelled software components. The first one (lines 49-54) determines the domain labels needed by the software component (line 51) and inserts a software component in a domain already created in the actual partitioning (Partitions) (lines 52-53), without increasing the number of domains of the new partitioning ([PMew|TmpPartitions]). The second clause (lines 55-62), after determining the domain labels needed by the software component (line 57), creates a new domain (P) hosting only the software component (line 59). Then, the number of domains of the partitioning is incremented (line 60) and it is checked that it is less than or equal to the domain limit (line 61). ```
49 partitioning([(S,TData,TChar)[Ss],Ndom,DLimit,Partitions,NewPartitions):-
50 software(S,_,_,_),
51 partitionCharLabel[(Char,TData,TCP),
52 select(((TData,TCP),Partitions,TmpPartitions),
53 Whewwwww((Data,TCP),[S]P)),
54 partitioning(Ss,Npar,Plimit,[PMew|TmpPartitions],NewPartitions). 55 partitioning([(S,TData,TChar)[Ss],Ndom,DLimit,Partitions,NewPartitions):-
56 software(S,_,_,_),
57 partitionCharLabel[(Char,TData,TCP),
58X+ member(((TData,TCP),_),Partitions),
59P=((TData,TCP),[S]),
60 Newtons is Modn +,
61 Newton = Ulimit,
62 partitioning(Ss,NewWdom,DLimit,[P|Partitions],NewPartitions). 63 partitioning([],_,_,P,P). ```  
**Listing 8**. The partitioning/5 predicate. Note that the two clauses are _not_ mutually exclusive, due to the missing check in the second clause about the presence in the partitioning of the domain needed by the software component (commented line 58). This creates a backtracking point in the Prolog engine search, covering all the possible partitionings with different queries that retrieve all the partitionings of an application that satisfy a specific labelling and have a limited number of domains. The cost/3 predicate is listed in Listing 9 and is used to calculate the cost c of migrating from the partitioning P1 to the partitioning P2. For each partitioning, the list of links is created to determine the status of every link (lines 65-66). The cost is then calculated by using the predicate linksCost/3 (line 67), which checks the link with different statuses and sums the migration cost of every software. ```
64 cost(P1,P2,C):-
65 links(P1,P1 links),
66 links(P2,P2 links),
67 linksCost(P1 links,P2 links,C). ```  
**Listing 9**. The cost/3 predicate.