2 * Copyright (c) 2007-2015, SUSE LLC
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Transaction ordering
20 #include "transaction.h"
26 struct s_TransactionElement {
27 Id p; /* solvable id */
28 Id edges; /* pointer into edges data */
32 struct s_TransactionOrderdata {
33 struct s_TransactionElement *tes;
40 #define TYPE_BROKEN (1<<0)
41 #define TYPE_CON (1<<1)
43 #define TYPE_REQ_P (1<<2)
44 #define TYPE_PREREQ_P (1<<3)
46 #define TYPE_SUG (1<<4)
47 #define TYPE_REC (1<<5)
49 #define TYPE_REQ (1<<6)
50 #define TYPE_PREREQ (1<<7)
52 #define TYPE_CYCLETAIL (1<<16)
53 #define TYPE_CYCLEHEAD (1<<17)
55 #define EDGEDATA_BLOCK 127
58 transaction_clone_orderdata(Transaction *trans, Transaction *srctrans)
60 struct s_TransactionOrderdata *od = srctrans->orderdata;
63 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
64 trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
65 trans->orderdata->ntes = od->ntes;
66 trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
67 trans->orderdata->ninvedgedata = od->ninvedgedata;
70 trans->orderdata->cycles = solv_calloc(1, sizeof(Queue));
71 queue_init_clone(trans->orderdata->cycles, od->cycles);
76 transaction_free_orderdata(Transaction *trans)
80 struct s_TransactionOrderdata *od = trans->orderdata;
81 od->tes = solv_free(od->tes);
82 od->invedgedata = solv_free(od->invedgedata);
85 queue_free(od->cycles);
86 od->cycles = solv_free(od->cycles);
88 trans->orderdata = solv_free(trans->orderdata);
94 struct s_TransactionElement *tes;
106 addteedge(struct orderdata *od, int from, int to, int type)
109 struct s_TransactionElement *te;
114 /* printf("edge %d(%s) -> %d(%s) type %x\n", from, pool_solvid2str(pool, od->tes[from].p), to, pool_solvid2str(pool, od->tes[to].p), type); */
117 for (i = te->edges; od->edgedata[i]; i += 2)
118 if (od->edgedata[i] == to)
120 /* test of brokenness */
121 if (type == TYPE_BROKEN)
122 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
125 od->edgedata[i + 1] |= type;
128 if (i + 1 == od->nedgedata)
130 /* printf("tail add %d\n", i - te->edges); */
133 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
137 /* printf("extend %d\n", i - te->edges); */
138 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
140 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
141 i = od->nedgedata + (i - te->edges);
142 te->edges = od->nedgedata;
144 od->edgedata[i] = to;
145 od->edgedata[i + 1] = type;
146 od->edgedata[i + 2] = 0; /* end marker */
147 od->nedgedata = i + 3;
152 addedge(struct orderdata *od, Id from, Id to, int type)
154 Transaction *trans = od->trans;
155 Pool *pool = trans->pool;
157 struct s_TransactionElement *te;
160 /* printf("addedge %d %d type %d\n", from, to, type); */
161 s = pool->solvables + from;
162 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
164 /* obsolete, map to install */
165 if (trans->transaction_installed[from - pool->installed->start] > 0)
166 from = trans->transaction_installed[from - pool->installed->start];
173 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
174 transaction_all_obs_pkgs(trans, from, &ti);
175 for (i = 0; i < ti.count; i++)
176 ret |= addedge(od, ti.elements[i], to, type);
181 s = pool->solvables + to;
182 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
184 /* obsolete, map to install */
185 if (trans->transaction_installed[to - pool->installed->start] > 0)
186 to = trans->transaction_installed[to - pool->installed->start];
193 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
194 transaction_all_obs_pkgs(trans, to, &ti);
195 for (i = 0; i < ti.count; i++)
196 ret |= addedge(od, from, ti.elements[i], type);
202 /* map from/to to te numbers */
203 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
210 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
216 return addteedge(od, i, to, type);
220 havescripts(Pool *pool, Id solvid)
222 Solvable *s = pool->solvables + solvid;
228 reqp = s->repo->idarraydata + s->requires;
229 while ((req = *reqp++) != 0)
231 if (req == SOLVABLE_PREREQMARKER)
238 dep = pool_id2str(pool, req);
239 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
247 addsolvableedges(struct orderdata *od, Solvable *s)
249 Transaction *trans = od->trans;
250 Pool *pool = trans->pool;
252 int i, j, pre, numins;
253 Repo *installed = pool->installed;
259 printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
261 p = s - pool->solvables;
266 reqp = s->repo->idarraydata + s->requires;
268 while ((req = *reqp++) != 0)
270 if (req == SOLVABLE_PREREQMARKER)
276 numins = 0; /* number of packages to be installed providing it */
277 provbyinst = 0; /* provided by kept package */
278 FOR_PROVIDES(p2, pp2, req)
280 s2 = pool->solvables + p2;
283 depq.count = 0; /* self provides */
286 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
291 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
292 continue; /* package stays uninstalled */
294 if (s->repo == installed)
296 /* s gets uninstalled */
297 queue_pushunique(&depq, p2);
298 if (s2->repo != installed)
303 if (s2->repo == installed)
304 continue; /* s2 gets uninstalled */
305 queue_pushunique(&depq, p2);
310 /* prune to harmless ->inst edges */
311 for (i = j = 0; i < depq.count; i++)
312 if (pool->solvables[depq.elements[i]].repo != installed)
313 depq.elements[j++] = depq.elements[i];
317 if (numins && depq.count)
319 if (s->repo == installed)
321 for (i = 0; i < depq.count; i++)
323 if (pool->solvables[depq.elements[i]].repo == installed)
325 for (j = 0; j < depq.count; j++)
327 if (pool->solvables[depq.elements[j]].repo != installed)
329 if (trans->transaction_installed[depq.elements[i] - pool->installed->start] == depq.elements[j])
330 continue; /* no self edge */
332 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, depq.elements[i]), pool_dep2str(pool, req), pool_solvid2str(pool, depq.elements[j]));
334 addedge(od, depq.elements[i], depq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
340 /* no mixed types, remove all deps on uninstalls */
341 for (i = j = 0; i < depq.count; i++)
342 if (pool->solvables[depq.elements[i]].repo != installed)
343 depq.elements[j++] = depq.elements[i];
346 for (i = 0; i < depq.count; i++)
348 p2 = depq.elements[i];
349 if (pool->solvables[p2].repo != installed)
351 /* all elements of depq are installs, thus have different TEs */
352 if (pool->solvables[p].repo != installed)
355 printf("add inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
357 addedge(od, p, p2, pre);
362 printf("add uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
364 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
369 if (s->repo != installed)
370 continue; /* no inst->uninst edges, please! */
372 /* uninst -> uninst edge. Those make trouble. Only add if we must */
373 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
375 /* p is obsoleted by another package and has no scripts */
376 /* we assume that the obsoletor is good enough to replace p */
380 printf("add uninst->uninst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
382 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
390 conp = s->repo->idarraydata + s->conflicts;
391 while ((con = *conp++) != 0)
393 FOR_PROVIDES(p2, pp2, con)
397 s2 = pool->solvables + p2;
400 if (s->repo == installed)
402 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
404 /* deinstall p before installing p2 */
406 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
408 addedge(od, p2, p, TYPE_CON);
413 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
415 /* deinstall p2 before installing p */
417 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
419 addedge(od, p, p2, TYPE_CON);
426 if (s->recommends && s->repo != installed)
429 recp = s->repo->idarraydata + s->recommends;
430 while ((rec = *recp++) != 0)
433 FOR_PROVIDES(p2, pp2, rec)
435 s2 = pool->solvables + p2;
438 depq.count = 0; /* self provides */
441 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
443 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
444 continue; /* package stays uninstalled */
445 if (s2->repo != installed)
446 queue_pushunique(&depq, p2);
448 for (i = 0; i < depq.count; i++)
450 p2 = depq.elements[i];
451 if (pool->solvables[p2].repo != installed)
454 printf("add recommends inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, rec), pool_solvid2str(pool, p2));
456 addedge(od, p, p2, TYPE_REC);
461 if (s->suggests && s->repo != installed)
464 sugp = s->repo->idarraydata + s->suggests;
465 while ((sug = *sugp++) != 0)
468 FOR_PROVIDES(p2, pp2, sug)
470 s2 = pool->solvables + p2;
473 depq.count = 0; /* self provides */
476 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
478 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
479 continue; /* package stays uninstalled */
480 if (s2->repo != installed)
481 queue_pushunique(&depq, p2);
483 for (i = 0; i < depq.count; i++)
485 p2 = depq.elements[i];
486 if (pool->solvables[p2].repo != installed)
489 printf("add suggests inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, sug), pool_solvid2str(pool, p2));
491 addedge(od, p, p2, TYPE_SUG);
496 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &depq) && depq.count)
498 /* we're getting deinstalled/updated. Try to do this before our
499 * triggers are hit */
500 for (i = 0; i < depq.count; i++)
502 Id tri = depq.elements[i];
503 FOR_PROVIDES(p2, pp2, tri)
507 s2 = pool->solvables + p2;
510 if (s2->name == s->name)
511 continue; /* obsoleted anyway */
512 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
514 /* deinstall/update p before installing p2 */
516 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
518 addedge(od, p2, p, TYPE_CON);
527 /* break an edge in a cycle */
529 breakcycle(struct orderdata *od, Id *cycle)
531 Pool *pool = od->trans->pool;
532 Id ddegmin, ddegmax, ddeg;
534 struct s_TransactionElement *te;
537 ddegmin = ddegmax = 0;
538 for (k = 0; cycle[k + 1]; k += 2)
540 ddeg = od->edgedata[cycle[k + 1] + 1];
543 if (!k || ddeg < ddegmin)
551 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
553 /* prefer k, as l comes from a package with contains scriptlets */
557 /* same edge value, check for prereq */
561 /* record brkoen cycle starting with the tail */
562 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
563 queue_push(&od->cycles, k / 2); /* cycle elements */
564 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
565 queue_push(&od->cycles, (ddegmax << 16) | ddegmin); /* max/min values */
572 queue_push(&od->cyclesdata, cycle[k]);
576 queue_push(&od->cyclesdata, 0); /* mark end */
578 /* break that edge */
579 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
582 if (ddegmin < TYPE_REQ)
586 /* cycle recorded, print it */
587 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
588 POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
589 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
590 for (k = 0; cycle[k + 1]; k += 2)
592 te = od->tes + cycle[k];
593 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
594 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
596 POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
598 POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
603 dump_tes(struct orderdata *od)
605 Pool *pool = od->trans->pool;
608 struct s_TransactionElement *te, *te2;
611 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
613 Solvable *s = pool->solvables + te->p;
614 POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
615 if (s->repo != pool->installed)
618 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
619 for (j = 0; j < obsq.count; j++)
620 POOL_DEBUG(SOLV_DEBUG_RESULT, " -%s\n", pool_solvid2str(pool, obsq.elements[j]));
622 for (j = te->edges; od->edgedata[j]; j += 2)
624 te2 = od->tes + od->edgedata[j];
625 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
626 POOL_DEBUG(SOLV_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
628 POOL_DEBUG(SOLV_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
635 reachable(struct orderdata *od, Id i)
637 struct s_TransactionElement *te = od->tes + i;
643 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
645 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
647 if (!od->tes[k].mark)
649 if (od->tes[k].mark == 2)
659 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
662 Transaction *trans = od->trans;
663 Pool *pool = trans->pool;
665 struct s_TransactionElement *te;
670 printf("addcycleedges\n");
671 for (i = 0; (j = cycle[i]) != 0; i++)
672 printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
675 /* first add all the tail cycle edges */
677 /* see what we can reach from the cycle */
679 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
681 for (i = 0; (j = cycle[i]) != 0; i++)
683 od->tes[j].mark = -1;
692 te->mark = te->mark < 0 ? 2 : 1;
693 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
695 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
697 if (od->tes[k].mark > 0)
698 continue; /* no need to visit again */
702 /* now all cycle TEs are marked with 2, all TEs reachable
703 * from the cycle are marked with 1 */
705 od->tes[tail].mark = 1; /* no need to add edges */
707 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
710 continue; /* reachable from cycle */
711 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
713 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
715 if (od->tes[k].mark != 2)
717 /* We found an edge to the cycle. Add an extra edge to the tail */
718 /* the TE was not reachable, so we're not creating a new cycle! */
720 printf("adding TO TAIL cycle edge %d->%d %s->%s!\n", i, tail, pool_solvid2str(pool, od->tes[i].p), pool_solvid2str(pool, od->tes[tail].p));
722 j -= te->edges; /* in case we move */
723 addteedge(od, i, tail, TYPE_CYCLETAIL);
725 break; /* one edge is enough */
729 /* now add all head cycle edges */
732 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
735 for (i = 0; (j = cycle[i]) != 0; i++)
740 /* first the head to save some time */
742 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
744 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
746 if (!od->tes[k].mark)
748 if (od->tes[k].mark == -1)
749 od->tes[k].mark = -2; /* no need for another edge */
751 for (i = 0; cycle[i] != 0; i++)
753 if (cycle[i] == head)
755 te = od->tes + cycle[i];
756 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
758 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
760 /* see if we can reach a cycle TE from k */
761 if (!od->tes[k].mark)
763 if (od->tes[k].mark == -1)
766 printf("adding FROM HEAD cycle edge %d->%d %s->%s [%s]!\n", head, k, pool_solvid2str(pool, od->tes[head].p), pool_solvid2str(pool, od->tes[k].p), pool_solvid2str(pool, od->tes[cycle[i]].p));
768 addteedge(od, head, k, TYPE_CYCLEHEAD);
769 od->tes[k].mark = -2; /* no need to add that one again */
776 transaction_order(Transaction *trans, int flags)
778 Pool *pool = trans->pool;
779 Queue *tr = &trans->steps;
780 Repo *installed = pool->installed;
783 int i, j, k, numte, numedge;
785 struct s_TransactionElement *te;
786 Queue todo, obsq, samerepoq, uninstq;
795 start = now = solv_timems(0);
796 POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
797 /* free old data if present */
798 if (trans->orderdata)
800 struct s_TransactionOrderdata *od = trans->orderdata;
801 od->tes = solv_free(od->tes);
802 od->invedgedata = solv_free(od->invedgedata);
803 trans->orderdata = solv_free(trans->orderdata);
806 /* create a transaction element for every active component */
808 for (i = 0; i < tr->count; i++)
811 s = pool->solvables + p;
812 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
816 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
818 return; /* nothing to do... */
820 numte++; /* leave first one zero */
821 memset(&od, 0, sizeof(od));
824 od.tes = solv_calloc(numte, sizeof(*od.tes));
825 od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
828 queue_init(&od.cycles);
831 for (i = 0, te = od.tes + 1; i < tr->count; i++)
834 s = pool->solvables + p;
835 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
841 /* create dependency graph */
842 for (i = 0; i < tr->count; i++)
843 addsolvableedges(&od, pool->solvables + tr->elements[i]);
847 for (i = 1, te = od.tes + i; i < numte; i++, te++)
848 for (j = te->edges; od.edgedata[j]; j += 2)
850 POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
851 POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
857 now = solv_timems(0);
858 /* kill all cycles */
860 for (i = numte - 1; i > 0; i--)
861 queue_push(&todo, i);
865 i = queue_pop(&todo);
866 /* printf("- look at TE %d\n", i); */
870 od.tes[i].mark = 2; /* done with that one */
875 continue; /* already finished before */
878 int edgestovisit = 0;
879 /* new node, visit edges */
880 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
882 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
884 if (od.tes[k].mark == 2)
885 continue; /* no need to visit again */
887 queue_push(&todo, -i); /* end of edges marker */
888 queue_push(&todo, k);
891 te->mark = 2; /* no edges, done with that one */
893 te->mark = 1; /* under investigation */
896 /* oh no, we found a cycle */
897 /* find start of cycle node (<0) */
898 for (j = todo.count - 1; j >= 0; j--)
899 if (todo.elements[j] == -i)
903 /* build te/edge chain */
905 for (j = k; j < todo.count; j++)
906 if (todo.elements[j] < 0)
907 todo.elements[k++] = -todo.elements[j];
908 cycel = k - cycstart;
910 /* make room for edges, two extra element for cycle loop + terminating 0 */
911 while (todo.count < cycstart + 2 * cycel + 2)
912 queue_push(&todo, 0);
913 cycle = todo.elements + cycstart;
914 cycle[cycel] = i; /* close the loop */
915 cycle[2 * cycel + 1] = 0; /* terminator */
916 for (k = cycel; k > 0; k--)
918 cycle[k * 2] = cycle[k];
919 te = od.tes + cycle[k - 1];
920 assert(te->mark == 1);
921 te->mark = 0; /* reset investigation marker */
922 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
923 for (j = te->edges; od.edgedata[j]; j += 2)
924 if (od.edgedata[j] == cycle[k])
926 assert(od.edgedata[j]);
927 cycle[k * 2 - 1] = j;
929 /* now cycle looks like this: */
930 /* te1 edge te2 edge te3 ... teN edge te1 0 */
931 breakcycle(&od, cycle);
932 /* restart with start of cycle */
933 todo.count = cycstart + 1;
935 POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
936 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
938 now = solv_timems(0);
939 /* now go through all broken cycles and create cycle edges to help
941 for (i = od.cycles.count - 4; i >= 0; i -= 4)
943 if (od.cycles.elements[i + 2] >= TYPE_REQ)
944 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
946 for (i = od.cycles.count - 4; i >= 0; i -= 4)
948 if (od.cycles.elements[i + 2] < TYPE_REQ)
949 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
951 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
956 /* all edges are finally set up and there are no cycles, now the easy part.
957 * Create an ordered transaction */
958 now = solv_timems(0);
959 /* first invert all edges */
960 for (i = 1, te = od.tes + i; i < numte; i++, te++)
961 te->mark = 1; /* term 0 */
962 for (i = 1, te = od.tes + i; i < numte; i++, te++)
964 for (j = te->edges; od.edgedata[j]; j += 2)
966 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
968 od.tes[od.edgedata[j]].mark++;
972 for (i = 1, te = od.tes + i; i < numte; i++, te++)
977 POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
978 od.invedgedata = solv_calloc(j + 1, sizeof(Id));
979 for (i = 1, te = od.tes + i; i < numte; i++, te++)
981 for (j = te->edges; od.edgedata[j]; j += 2)
983 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
985 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
988 for (i = 1, te = od.tes + i; i < numte; i++, te++)
989 te->edges = te->mark; /* edges now points into invedgedata */
990 od.edgedata = solv_free(od.edgedata);
991 od.nedgedata = j + 1;
993 /* now the final ordering */
994 for (i = 1, te = od.tes + i; i < numte; i++, te++)
996 for (i = 1, te = od.tes + i; i < numte; i++, te++)
997 for (j = te->edges; od.invedgedata[j]; j++)
998 od.tes[od.invedgedata[j]].mark++;
1000 queue_init(&samerepoq);
1001 queue_init(&uninstq);
1003 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1006 if (installed && pool->solvables[te->p].repo == installed)
1007 queue_push(&uninstq, i);
1009 queue_push(&todo, i);
1011 assert(todo.count > 0 || uninstq.count > 0);
1012 oldcount = tr->count;
1019 temedianr = solv_calloc(numte, sizeof(Id));
1020 for (i = 1; i < numte; i++)
1022 Solvable *s = pool->solvables + od.tes[i].p;
1023 if (installed && s->repo == installed)
1026 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1031 /* select an TE i */
1033 i = queue_shift(&uninstq);
1034 else if (samerepoq.count)
1035 i = queue_shift(&samerepoq);
1036 else if (todo.count)
1038 /* find next repo/media */
1039 for (j = 0; j < todo.count; j++)
1041 if (!j || temedianr[todo.elements[j]] < lastmedia)
1044 lastmedia = temedianr[todo.elements[j]];
1047 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1049 /* move all matching TEs to samerepoq */
1050 for (i = j = 0; j < todo.count; j++)
1052 int k = todo.elements[j];
1053 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1054 queue_push(&samerepoq, k);
1056 todo.elements[i++] = k;
1060 assert(samerepoq.count);
1061 i = queue_shift(&samerepoq);
1067 queue_push(tr, te->p);
1069 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1071 for (j = te->edges; od.invedgedata[j]; j++)
1073 struct s_TransactionElement *te2 = od.tes + od.invedgedata[j];
1074 assert(te2->mark > 0);
1075 if (--te2->mark == 0)
1077 Solvable *s = pool->solvables + te2->p;
1079 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1081 if (installed && s->repo == installed)
1082 queue_push(&uninstq, od.invedgedata[j]);
1083 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1084 queue_push(&samerepoq, od.invedgedata[j]);
1086 queue_push(&todo, od.invedgedata[j]);
1090 solv_free(temedianr);
1092 queue_free(&samerepoq);
1093 queue_free(&uninstq);
1095 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1096 assert(te->mark == 0);
1098 /* add back obsoleted packages */
1099 transaction_add_obsoleted(trans);
1100 assert(tr->count == oldcount);
1102 POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1103 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1105 if ((flags & (SOLVER_TRANSACTION_KEEP_ORDERDATA | SOLVER_TRANSACTION_KEEP_ORDERCYCLES)) != 0)
1107 struct s_TransactionOrderdata *tod;
1108 trans->orderdata = tod = solv_calloc(1, sizeof(*trans->orderdata));
1109 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERCYCLES) != 0)
1111 Queue *cycles = tod->cycles = solv_calloc(1, sizeof(Queue));
1112 queue_init_clone(cycles, &od.cyclesdata);
1113 /* map from tes to packages */
1114 for (i = 0; i < cycles->count; i++)
1115 if (cycles->elements[i])
1116 cycles->elements[i] = od.tes[cycles->elements[i]].p;
1117 queue_insertn(cycles, cycles->count, od.cycles.count, od.cycles.elements);
1118 queue_push(cycles, od.cycles.count / 4);
1120 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1124 tod->invedgedata = od.invedgedata;
1125 tod->ninvedgedata = od.nedgedata;
1131 solv_free(od.invedgedata);
1132 queue_free(&od.cycles);
1133 queue_free(&od.cyclesdata);
1138 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1141 struct s_TransactionOrderdata *od = trans->orderdata;
1142 struct s_TransactionElement *te;
1145 return choices->count;
1148 /* initialization step */
1149 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1151 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1153 for (j = te->edges; od->invedgedata[j]; j++)
1154 od->tes[od->invedgedata[j]].mark++;
1156 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1158 queue_push(choices, te->p);
1159 return choices->count;
1161 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1162 if (te->p == chosen)
1165 return choices->count;
1168 /* hey! out-of-order installation! */
1171 for (j = te->edges; od->invedgedata[j]; j++)
1173 te = od->tes + od->invedgedata[j];
1174 assert(te->mark > 0 || te->mark == -1);
1175 if (te->mark > 0 && --te->mark == 0)
1176 queue_push(choices, te->p);
1178 return choices->count;
1182 transaction_add_obsoleted(Transaction *trans)
1184 Pool *pool = trans->pool;
1185 Repo *installed = pool->installed;
1192 if (!installed || !trans->steps.count)
1194 /* calculate upper bound */
1196 FOR_REPO_SOLVABLES(installed, p, s)
1197 if (MAPTST(&trans->transactsmap, p))
1202 steps = &trans->steps;
1203 queue_insertn(steps, 0, max, 0);
1206 map_init(&done, installed->end - installed->start);
1208 for (j = 0, i = max; i < steps->count; i++)
1210 p = trans->steps.elements[i];
1211 if (pool->solvables[p].repo == installed)
1213 if (!trans->transaction_installed[p - pool->installed->start])
1214 trans->steps.elements[j++] = p;
1217 trans->steps.elements[j++] = p;
1219 transaction_all_obs_pkgs(trans, p, &obsq);
1220 for (k = 0; k < obsq.count; k++)
1222 p = obsq.elements[k];
1223 assert(p >= installed->start && p < installed->end);
1224 if (!MAPTST(&trans->transactsmap, p)) /* just in case */
1226 if (MAPTST(&done, p - installed->start))
1228 MAPSET(&done, p - installed->start);
1229 trans->steps.elements[j++] = p;
1233 /* free unneeded space */
1234 queue_truncate(steps, j);
1240 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1242 Pool *pool = trans->pool;
1247 if (MAPTST(seen, pkg))
1250 s = pool->solvables + pkg;
1252 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1258 reqp = s->repo->idarraydata + s->requires;
1259 while ((req = *reqp++) != 0)
1261 if (req == SOLVABLE_PREREQMARKER)
1266 if (onlyprereq && !inpre)
1268 if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
1271 /* first check kept packages, then freshly installed, then not yet uninstalled */
1272 FOR_PROVIDES(p, pp, req)
1274 if (!MAPTST(ins, p))
1276 if (MAPTST(&trans->transactsmap, p))
1279 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1283 FOR_PROVIDES(p, pp, req)
1285 if (!MAPTST(ins, p))
1287 if (pool->solvables[p].repo == pool->installed)
1290 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1295 FOR_PROVIDES(p, pp, req)
1297 if (!MAPTST(ins, p))
1300 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
1305 POOL_DEBUG(SOLV_DEBUG_RESULT, " %c%s: nothing provides %s needed by %c%s\n", pool->solvables[tepkg].repo == pool->installed ? '-' : '+', pool_solvid2str(pool, tepkg), pool_dep2str(pool, req), s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1312 transaction_check_order(Transaction *trans)
1314 Pool *pool = trans->pool;
1320 POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
1321 map_init(&ins, pool->nsolvables);
1322 map_init(&seen, pool->nsolvables);
1323 if (pool->installed)
1325 FOR_REPO_SOLVABLES(pool->installed, p, s)
1329 for (i = 0; i < trans->steps.count; i++)
1331 p = trans->steps.elements[i];
1332 s = pool->solvables + p;
1333 if (s->repo != pool->installed)
1335 if (s->repo != pool->installed)
1337 if (havescripts(pool, p))
1340 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
1342 if (s->repo == pool->installed)
1347 POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");
1351 transaction_order_get_cycleids(Transaction *trans, Queue *q, int minseverity)
1353 struct s_TransactionOrderdata *od = trans->orderdata;
1355 int i, cid, ncycles;
1358 if (!od || !od->cycles || !od->cycles->count)
1361 ncycles = cq->elements[cq->count - 1];
1362 i = cq->count - 1 - ncycles * 4;
1363 for (cid = 1; cid <= ncycles; cid++, i += 4)
1367 int cmin = cq->elements[i + 3] & 0xffff;
1368 int cmax = (cq->elements[i + 3] >> 16) & 0xffff;
1369 if (minseverity >= SOLVER_ORDERCYCLE_NORMAL && cmin < TYPE_REQ)
1371 if (minseverity >= SOLVER_ORDERCYCLE_CRITICAL && (cmax & TYPE_PREREQ) == 0)
1379 transaction_order_get_cycle(Transaction *trans, Id cid, Queue *q)
1381 struct s_TransactionOrderdata *od = trans->orderdata;
1383 int cmin, cmax, severity;
1387 if (!od || !od->cycles || !od->cycles->count)
1388 return SOLVER_ORDERCYCLE_HARMLESS;
1390 ncycles = cq->elements[cq->count - 1];
1391 if (cid < 1 || cid > ncycles)
1392 return SOLVER_ORDERCYCLE_HARMLESS;
1393 cid = cq->count - 1 - 4 * (ncycles - cid + 1);
1394 cmin = cq->elements[cid + 3] & 0xffff;
1395 cmax = (cq->elements[cid + 3] >> 16) & 0xffff;
1396 if (cmin < TYPE_REQ)
1397 severity = SOLVER_ORDERCYCLE_HARMLESS;
1398 else if ((cmax & TYPE_PREREQ) == 0)
1399 severity = SOLVER_ORDERCYCLE_NORMAL;
1401 severity = SOLVER_ORDERCYCLE_CRITICAL;
1403 queue_insertn(q, 0, cq->elements[cid + 1], cq->elements + cq->elements[cid]);